-
Notifications
You must be signed in to change notification settings - Fork 0
/
Number_Gussing_Game---Oasis_Task-2.java
86 lines (85 loc) · 3.81 KB
/
Number_Gussing_Game---Oasis_Task-2.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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
import java.util.*;
public class NumberGuessingGame {
static ArrayList<Integer> scoreBoard = new ArrayList<Integer>();
public static void main(String[] args) {
NumberGuessingGame methodChange = new NumberGuessingGame();
methodChange.menu(scoreBoard);
}
public void menu(ArrayList<Integer> scoreBoard) {
NumberGuessingGame methodChange = new NumberGuessingGame();
Scanner input = new Scanner(System.in);
System.out.println("--------------------------------------------------------------------------------");
System.out.println("Welcome to the Number Gussing Game...!!!");
System.out.println("1. Play the Game...");
System.out.println("2. Score Board...");
System.out.println("3. Exit the game...");
System.out.println("--------------------------------------------------------------------------------");
try {
System.out.print("What you want to do from the above options ( 1 / 2 / 3 ) ? ---> ");
int menuOption = input.nextInt();
switch (menuOption) {
case 1:
System.out.print("\n"+"What would you like the range of the numbers to be? ---> ");
int numberRange = input.nextInt();
int randomNumber = methodChange.randomNumber(numberRange);
methodChange.guessNumber(randomNumber);
break;
case 2:
methodChange.displayScoreBoard();
break;
case 3:
System.out.println("\n"+"Thanks for playing the game...!!!");
System.exit(1);
break;
default:
throw new InputMismatchException("Invalid number entry.!!! Could you please try again...");
}
}catch(InputMismatchException e){
System.err.println("\n"+"Invalid number entry.!!! Could you please try again..."+"\n");
menu(scoreBoard);
}
}
public int randomNumber(int numberRange) {
Random random = new Random();
int randomNumber = random.nextInt(numberRange) + 1;
return randomNumber;
}
public void guessNumber(int randomNumber) {
Scanner input = new Scanner(System.in);
int userGuess;
int guess = 0;
do {
System.out.print("Enter your guess number: ");
userGuess = input.nextInt();
guess++;
if (userGuess > randomNumber) {
System.out.println("Lower...");
} else if (userGuess < randomNumber) {
System.out.println("Higher...");
}
} while (randomNumber != userGuess);
System.out.println(" ");
if (guess == 1) {
System.out.println("You answered number is right in " + guess + " try!");
} else {
System.out.println("You answered number is right in " + guess + " tries!");
}
scoreBoard.add(guess);
System.out.println(" ");
menu(scoreBoard);
}
public void displayScoreBoard() {
System.out.println("--------------------------------------------------------------------------------");
System.out.println("Score Board ---> ");
System.out.println("------------");
Collections.sort(scoreBoard);
System.out.println("Your fastest game score today out of all tries is: In " + scoreBoard.get(0) + " try.!" + "\n");
System.out.println("All scores ---> ");
System.out.println("-----------");
for (Integer scores : scoreBoard) {
System.out.println("Finished the number game in " + scores + " tries");
}
System.out.println(" ");
menu(scoreBoard);
}
}