-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMain.java
62 lines (50 loc) · 1.8 KB
/
Main.java
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
package com.mzeechalle.guessmegame;
import java.util.Random;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
System.out.println("Welcome to Wonderland!");
System.out.println("May I have your name?");
Scanner scanner = new Scanner(System.in);
String name = scanner.next();
System.out.println("Hello " + name);
int beginAnswer = 0;
while (beginAnswer != 1) {
System.out.println("Shall we begin?");
System.out.println("\t1. Yes");
System.out.println("\t2. No");
beginAnswer = scanner.nextInt();
}
Random random = new Random();
int x = random.nextInt(20) + 1;
System.out.println("Please guess a number: ");
int userInput = scanner.nextInt();
int timesTried = 0;
boolean hasWon = false;
boolean shouldFinish = false;
while(!shouldFinish) {
timesTried++;
if (timesTried < 5) {
if (userInput == x ) {
hasWon = true;
shouldFinish = true;
}else if (userInput < x) {
System.out.println("Guess lower");
userInput = scanner.nextInt();
}else{
System.out.println("Guess higher");
userInput = scanner.nextInt();
}
}else{
shouldFinish = true;
}
}
if (hasWon) {
System.out.println("Congratulations! you have guessed in your " + timesTried + " " + "guess.");
}
else {
System.out.println("Game Over!");
System.out.println("The number was: " + x);
}
}
}