Skip to content
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Can convert feet to centimeters and vice-versa!
  • Loading branch information
kylewehrung committed Jul 21, 2023
commit 623e6cd4cdca60a84467e2c195b18a4976a5a5b7
19 changes: 7 additions & 12 deletions ch03/Convert.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,29 +29,24 @@ public static void main(String[] args) {
feet = inches / IN_PER_FOOT;
remainder = inches % IN_PER_FOOT;
inches = inches - (feet * IN_PER_FOOT);
System.out.printf("%.2f cm = %fd ft, %d in\n", cm, feet, inches);
System.out.printf("%.2f cm = %d ft, %d in\n", cm, feet, inches);
}
} else if (choice == 2); {
} else if (choice == 2) {
System.out.print("Enter feet: ");
feet = in.nextInt();
System.out.print("Enter inches: ");
inches = in.nextInt();

if (feet < 0 || inches < 0 || inches >= IN_PER_FOOT) {
System.out.println("Please enter valid and positive values for feet and inches.");
} else {
cm = (feet * IN_PER_FOOT + inches) * CM_PER_INCH;
System.out.printf("%d ft, %d in = %.2f cm\n", feet, inches, cm);
}
} else {
System.out.println("Invalid choice, please enter 1 or 2.");
}

// prompt the user and get the value
System.out.print("Exactly how many cm? ");
cm = in.nextDouble();

// convert and output the result
inches = (int) (cm / CM_PER_INCH);
feet = inches / IN_PER_FOOT;
remainder = inches % IN_PER_FOOT;
System.out.printf("%.2f cm = %d ft, %d in\n",
cm, feet, remainder);
}

}