forked from AllenDowney/ThinkJavaCode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNumberGuess.java
More file actions
61 lines (51 loc) · 1.33 KB
/
NumberGuess.java
File metadata and controls
61 lines (51 loc) · 1.33 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
import java.util.Scanner;
public class NumberGuess
{
public static void main(String[] args)
{
playGames();
}
private static void playGames()
{
boolean playerWantsToKeepPlaying;
do
{
playgame();
playerWantsToKeepPlaying = getYesOrNoFromUser("Want to play again?");
}while(playerWantsToKeepPlaying);
}
public static void playgame()
{
Die die = new Die();
System.out.println("I am going to roll a die");
die.roll();
System.out.println("Can you guess the number?");
int guess = getNumberFromUser();
if (guess == die.getValue())
{
System.out.println("You got it!");
}
else
{
System.out.println("Wrong! No soup for you! See you next year?");
}
}
private static int getNumberFromUser ()
{
Scanner in = new Scanner(System.in);
int number = in.nextInt();
return number;
}
private static boolean getYesOrNoFromUser (String question)
{
System.out.println(question);
Scanner in = new Scanner(System.in);
String answer = in.nextLine();
boolean yes = false;
if(answer.equalsIgnoreCase("Yes"))
{
yes = true;
}
return yes;
}
}