forked from AllenDowney/ThinkJavaCode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGreatestNumber.java
More file actions
84 lines (59 loc) · 1.93 KB
/
GreatestNumber.java
File metadata and controls
84 lines (59 loc) · 1.93 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
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
import java.util.Random;
import java.util.Scanner;
public class GreatestNumber
{
public static void main(String[] args)
{
Scanner input = new Scanner(System.in);
System.out.print("Please Enter Your Name: ");
String yourName = input.next();
int numberOfGuesses;
numberOfGuesses = guessNumber();
System.out.println("Congrats, " + yourName + "!!!");
playAgain();
}
public static int guessNumber()
{
Random randomNumber = new Random();
Scanner input = new Scanner(System.in);
System.out.print("Please Guess A Number: ");
int yourGuess = input.nextInt();
int random = randomNumber.nextInt(100) + 1;
int guesses = 0;
int totalGuess = 0;
while (random != yourGuess)
{
if (yourGuess > random)
{
System.out.println("You are high");
}
else if (yourGuess < random)
{
System.out.println("You are low");
}
System.out.print("Please Guess Again: ");
yourGuess = input.nextInt();
guesses++;
totalGuess = totalGuess + guesses;
}
System.out.println("You are correct");
guesses++;
System.out.println("You guessed " + guesses + " times!");
return guesses;
}
public static void playAgain()
{
Scanner input = new Scanner(System.in);
System.out.println("Do you want to play again? Type 'yes' or 'no'.");
String playAgainAnswer = input.next();
int gamesPlayed = 1;
while (playAgainAnswer.equals("yes"))
{
guessNumber();
System.out.println("Do you want to play again? Type 'yes' or 'no'.");
playAgainAnswer = input.next();
gamesPlayed++;
}
System.out.println("You played " + gamesPlayed + " games");
}
}