-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathguessingGame.java
More file actions
48 lines (39 loc) · 1.56 KB
/
guessingGame.java
File metadata and controls
48 lines (39 loc) · 1.56 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
/**
* guessingGame
* This program is a simple game where the user tries to guess a random number between 1 and 10
* @Dan Joshwa
*/
import java.util.*;
import java.util.Random;
public class guessingGame
{
public static void main(String args[])
{
String playAgain;//this variable decides whether the player plays again or not
Random rnd = new Random(); //how random numbers are made
while (1==1)//infinite loop
{
int n1 = 1+rnd.nextInt(10);//this is the computer's random number
//String playAgain;
Scanner scan = new Scanner(System.in);//this gets user inputs
System.out.println("This is the one and only guessing game");//infor for user
System.out.println("Please guess a number between 1 and 10");
int guessInt = scan.nextInt();//user guess
if (guessInt == n1)//if the guess is right
{
System.out.println("Your guess was correct");
System.out.println("Would you like to play again? (Y/N)");
}
else//otherwise
{
System.out.println("Your guess wasn't correct");
System.out.println("Would you like to play agian? (Y/N)");
}
playAgain = scan.next();//whether the user wants to play again or not
if (playAgain.equals("N") || playAgain.equals("n"))//if not
{
System.exit(0);//cut the program
}
}
}
}