-
Notifications
You must be signed in to change notification settings - Fork 17
Expand file tree
/
Copy pathExercise.java
More file actions
38 lines (29 loc) · 989 Bytes
/
Exercise.java
File metadata and controls
38 lines (29 loc) · 989 Bytes
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
import java.util.Random;
import java.util.Scanner;
public class Exercise {
public static void main(String[] args) {
@SuppressWarnings("resource")
Scanner sc = new Scanner(System.in);
Random rnd = new Random();
int randomNumber = rnd.nextInt(100) + 1;
int attempts = 0;
boolean loop;
do {
attempts++;
System.out.print("Gib bitte Deinen Tipp ein: ");
int tip = sc.nextInt();
if (tip == randomNumber) {
System.out.println("Richtig. Du hast " + attempts + " Versuche benoetigt");
break;
// return
} else if (tip > randomNumber) {
System.out.println("Leider falsch, die gesuchte Zahl ist kleiner");
} else {
System.out.println("Leider falsch, die gesuchte Zahl ist groesser");
}
System.out.print("Moechtest Du nochmals raten (true, false)?: ");
loop = sc.nextBoolean();
} while (loop);
System.out.println("Die gesuchte Zahl war " + randomNumber);
}
}