-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLoopExample.java
More file actions
77 lines (62 loc) · 3.19 KB
/
Copy pathLoopExample.java
File metadata and controls
77 lines (62 loc) · 3.19 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
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
75
76
77
import java.util.Scanner;
/**
* Identifies a scenario that may require two different types of loops.
*/
public class LoopExample {
/**
* This method demonstrates a scenario where two different types of loops are used.
* The do-while loop gets user input and keeps looping until the user enters 'exit'.
* The for each loop is used to iterate over each character in the input string to validate it is a digit.
*
* @param args Console arguments. Not used in this example.
*/
public static void main(String[] args) {
// Initialize variables.
// The Scanner is used to read user input.
// The input is stored in the 'input' variable.
// The input will be 'valid' if it is a positive integer.
Scanner scanner = new Scanner(System.in);
String input;
boolean valid = false;
// A do-while loop loops until the condition in the while statement is false.
// In Java, an exclamation mark (!) means NOT.
do {
// Prompt the user to enter a positive integer.
System.out.print("Enter a positive integer (or 'exit' to quit): ");
// Get the input from the user and store it in the 'input' variable.
input = scanner.nextLine();
// If the input is 'exit', break out of the do-while loop.
if (input.equalsIgnoreCase("exit")) {
break;
}
// This is a for each loop, which iterates over each character in the input string.
// Checks if each character in the input is a digit [0-9]
// If it is not, print an error message and sets 'valid' to false, so it won't try to multiply the number.
for (char chr : input.toCharArray()) {
// Set valid to true. If all characters in the input are digits, it will remain true.
valid = true;
// If any character in the input is not a digit, print an error message and set 'valid' to false.
// Break out of the for-each loop immediately if input contains non-digits.
if (!Character.isDigit(chr)) {
System.out.println("Invalid input. Please enter a positive integer.");
valid = false;
break;
}
}
// If the input is valid, convert it to a float.
// Print the parsed number divided by 10.
if (valid) {
// Convert the input to a float.
float number = Float.parseFloat(input);
// Print the input number divided by 10.
System.out.printf("The number divided by 10 is: %.1f%n", number / 10);
}
// This line continues to loop the do-while loop unless the input is 'exit'.
} while (!input.equalsIgnoreCase("exit"));
scanner.close();
// Prints some end-of-program stuff.
System.out.println("\nThank you for using the divide by ten system. Goodbye!");
System.out.println("Please leave a rating of your experience in a brief 30 minute survey.");
System.out.println("We're duped into thinking these surveys give us good, mine-able data.");
}
}