Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Java practice #6

Open
wants to merge 18 commits into
base: master
Choose a base branch
from
Open
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
Next Next commit
Making it convert from inchs to centimeters as well
kylewehrung committed Jul 21, 2023

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature.
commit cd4c4ea65e62d68e41b98e1dba7e75c3a7fe12aa
30 changes: 30 additions & 0 deletions ch03/Convert.java
Original file line number Diff line number Diff line change
@@ -12,6 +12,36 @@ public static void main(String[] args) {
final int IN_PER_FOOT = 12;
Scanner in = new Scanner(System.in);

System.out.print("Enter 1 to convert centimeters to feet and inches\n"
+ "Enter 2 to convert feet and inches to centimeters\n"
+ "Your choice: ");

int choice = in.nextInt();

if (choice == 1) {
System.out.print("Exactly how many cm? ");
cm = in.nextDouble();

if (cm < 0) {
System.out.print("Please enter a positive value for centimeters.");
} else {
inches = (int) (cm / CM_PER_INCH);
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);
}
} 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.");
}
}

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