-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathguessingNumberGame.java
69 lines (46 loc) · 1.58 KB
/
guessingNumberGame.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
63
64
65
package com.nguyen;
import java.util.Random;
import java.util.Scanner;
public class guessingNumberGame {
// Function that implements the number guessing game
public static void guessingNumberGame() {
String userInput;
do {
Random randGen = new Random();
int randNum = randGen.nextInt(100) + 1;
int maxGuess = 10;
int score = 10;
Scanner scnr = new Scanner(System.in);
System.out.println("Guess any integer number from 1 to 100 (You have 10 guesses equivalent with 10 scores. One wrong guess will deduct one score):");
int i;
for (i = 0; i < maxGuess; i++) {
int userNum = scnr.nextInt();
if (userNum == randNum) {
System.out.println("Congratulation! You pick the correct number.");
break;
}
else if (userNum < randNum) {
score -= 1;
System.out.println("Sorry the guess is too low. You have " + score + " guess(es) left.");
}
//Number of guesses left equals score
else if (userNum > randNum) {
score -= 1;
System.out.println("Sorry the guess is too high. You have " + score + " guess(es) left.");
}
}
if (i >= maxGuess) {
System.out.println("You reached the maximum guess. You lost! The correct number was: " + randNum);
}
System.out.println("Do you want to play another game? (y/n)");
userInput = scnr.next();
}
while (userInput.equals("y"));
System.out.println("See you later!");
}
// Driver Code
public static void main(String arg[]) {
// Function Call
guessingNumberGame();
}
}