Repository navigation
Expand file tree
/
Copy pathLesson15_weightConverter.java
More file actions
48 lines (40 loc) · 1.64 KB
/
Copy pathLesson15_weightConverter.java
File metadata and controls
48 lines (40 loc) · 1.64 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
import java.util.Scanner;
public class Lesson15_weightConverter {
public static void main(String[] args) {
// WEIGHT CONVERSION PROGRAM
// Declare variables
Scanner sc = new Scanner(System.in);
// Welcome message
System.out.println("Welcome to the Weight Conversion Program (W.C.P)!");
System.out.println("To get started, please select an option!");
System.out.println("----------------------------------------------------");
System.out.println("1. Convert lbs to kgs");
System.out.println("2. Convert kgs to lbs");
// Prompt for user choice
System.out.print("Select your option: ");
int choice = Integer.parseInt(sc.nextLine());
switch(choice) {
// Option 1 convert lbs to kgs
case 1: {
System.out.print("Enter your weight in lbs: ");
double weight = Double.parseDouble(sc.nextLine());
double newWeight = weight * 0.453592;
System.out.printf("The new weight in kgs is: %.2f", newWeight);
break;
}
// Option 2 convert kgs to lbs
case 2: {
System.out.print("Enter your weight in kgs: ");
double weight = Double.parseDouble(sc.nextLine());
double newWeight = weight * 2.20462;
System.out.printf("The new weight in lbs is: %.2f", newWeight);
break;
}
// Else print not a valid choice
default: {
System.out.println("Not a valid choice!");
}
}
sc.close();
}
}