-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWeather.java
More file actions
61 lines (55 loc) · 2.03 KB
/
Copy pathWeather.java
File metadata and controls
61 lines (55 loc) · 2.03 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
/*
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, write the word sunny.
* If the letter is between J and P, write the word cloudy.
* If the letter is between Q and X, write the word rainy.
* If the input is any other letter, returns a statement that it is not a valid option.
*/
public class Weather {
/**
* 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 J and P, returns cloudy.
* If the letter is between Q and X, 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 > 88 || (69 < asciiValue && asciiValue < 74)) {
return "It is not a valid option";
}
if (asciiValue <= 69) { // 65 - 69; A to E
return "sunny";
} else if (asciiValue <= 80) { // 75 to 80; J to P
return "cloudy";
}
return "rainy"; // 81 to 88; Q to X
}
/**
* 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: ");
String input = scanner.nextLine();
if (input.length() == 1) {
scanner.close();
return input.charAt(0);
}
scanner.close();
return -1;
}
public static void main(String[] args) {
System.out.printf("%s\n", weather(getInput()));
}
}