-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNibbleMonsterWithLoopTillFull.java
More file actions
31 lines (25 loc) · 1.11 KB
/
NibbleMonsterWithLoopTillFull.java
File metadata and controls
31 lines (25 loc) · 1.11 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
import java.util.Random;
import java.util.Scanner;
public class NibbleMonsterWithLoopTillFull {
public static void main(String[] args) {
Random rand = new Random();
int hunger = rand.nextInt(501); // Generate a random hunger level between 0 and 500
System.out.println("The monster's hunger level is: " + hunger);
Scanner scanner = new Scanner(System.in);
while (hunger > 0) {
System.out.print("Feed the monster a nibble: ");
char input = scanner.next().charAt(0);
if (Character.isDigit(input) || (input >= 'a' && input <= 'f') || (input >= 'A' && input <= 'F')) {
int nibbleValue = (int) input;
hunger -= nibbleValue;
System.out.println("Monster's hunger decreased to: " + hunger);
} else {
int pukeValue = (int) input;
hunger += pukeValue;
System.out.println("Oops! Monster puked. Hunger increased to: " + hunger);
}
}
System.out.println("The monster is full! Hunger level is zero.");
scanner.close();
}
}