Repository navigation
Expand file tree
/
Copy pathComplexWeather.java
More file actions
75 lines (69 loc) · 2.64 KB
/
Copy pathComplexWeather.java
File metadata and controls
75 lines (69 loc) · 2.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
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
/*
Module 2: Critical Thinking Assignment
Adding Variables to Pseudocode
*/
import java.util.Scanner;
/**
* Asks a user to enter a letter.
* If the letter is between A and E, returns sunny.
* If the letter is between F and I, returns partly cloudy.
* If the letter is between J and P, returns cloudy.
* If the letter is between Q and X, returns light rain.
* If the letter is between Y and Z, returns rainy.
* If the value is any other letter, returns a statement that it is not a valid option.
*/
public class ComplexWeather {
/**
* Predicts the weather based on the Unicode value of a character (case-sensitive).
* If the letter is between A and E, returns sunny.
* If the letter is between F and I, returns partly cloudy.
* If the letter is between J and P, returns cloudy.
* If the letter is between Q and X, returns light rain.
* If the letter is between Y and Z, returns rainy.
* If the value is any other letter, returns a statement that it is not a valid option.
*
* @param asciiValue the Unicode value of the input character.
* @return The predicted weather based on the Unicode value of the input character.
*/
public static String weather(int asciiValue) {
if (asciiValue < 65 || asciiValue > 90) {
return "It is not a valid option";
}
if (asciiValue <= 69) { // 65 - 69; A to E
return "sunny";
} else if (asciiValue <= 74) { // 70 - 73; F to I
return "partly cloudy";
} else if (asciiValue <= 80) { // 74 to 80; J to P
return "cloudy";
} else if (asciiValue <= 88) { // 81 - 88; Q to X
return "light rain";
} else { // 89 to 90; Y to Z
return "rainy";
}
}
/**
* Prompts the user for an input and returns its Unicode value.
* Returns 0 if the input is not a single character.
*
* @return the Unicode value of the input character, or 0 if it is not a single character.
*/
public static int getInput() {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter a letter: (or 'exit' to quit) ");
String input = scanner.nextLine().toUpperCase();
if (input.equalsIgnoreCase("exit")) {
System.out.println("Exiting the program...");
scanner.close();
System.exit(0);
}
if (input.length() == 1) {
return input.charAt(0);
}
return -1;
}
public static void main(String[] args) {
while (true) {
System.out.printf("%s\n", weather(getInput()));
}
}
}