-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathLabInput2.java
74 lines (72 loc) · 2.44 KB
/
LabInput2.java
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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
/**
* LabInput2.java
* Time Interpreter Activity #2
* @author PP-Namias
* Licensed under MIT License
*/
/**
* @param [input] the scanner for user input.
* @param [num] the number that indicates the number of that needs to be interperted.
* @param [hundreds] the number of hundreds.
* @param [tens] the number of tens.
* @param [ones] the number of ones.
* @param [exit] the boolean to check if the program will exit.
*/
/*
* Write a program that inputs a three-digit value into variable num
* and then display each digit with the corresponding place value. A
* sample output is given below. Use LabInput2 as classname and filename
*
* Test output:
* Please enter a three-digit integer: 487
* The value of num = 487
* 4 hundreds 8 tens and 7 ones
*/
import java.util.Scanner;
public class LabInput2 {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int num, hundreds, tens, ones;
boolean exit = false;
while (!exit) {
try {
System.out.print("Please enter a three-digit integer or enter '0' to quit: ");
num = input.nextInt();
if (num == 0) {
System.out.print("Exiting the program! Thank you for using this program!");
exit = true;
}
else {
hundreds = num / 100;
tens = (num % 100) / 10;
ones = (num % 100) % 10;
System.out.println("The value of num = " + num);
if (hundreds == 0) {
System.out.println(tens + " tens and " + ones + " ones");
}
else if (hundreds == 1) {
System.out.println(hundreds + " hundred " + tens + " tens and " + ones + " ones");
}
else if (tens == 0) {
System.out.println(hundreds + " hundreds and " + ones + " ones");
}
else if (tens == 1) {
System.out.println(hundreds + " hundreds " + tens + " ten and " + ones + " ones");
}
else if (ones == 0) {
System.out.println(hundreds + " hundreds " + tens + " tens");
}
else if (ones == 1) {
System.out.println(hundreds + " hundreds " + tens + " tens and " + ones + " one");
}
else{
System.out.println(hundreds + " hundreds " + tens + " tens and " + ones + " ones");
}
}
} catch (Exception e) {
System.out.println("Please enter the correct input.");
}
}
input.close();
}
}