-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrandomNumberGame.java
76 lines (45 loc) · 1.73 KB
/
randomNumberGame.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
package javaapplication45;
import java.util.InputMismatchException;
import java.util.Random;
import java.util.Scanner;
public class randomNumberGame {
public static void main(String[] args) {
Random random =new Random();
Scanner keyboard =new Scanner(System.in);
int randomNumber;
int input=0;
int numberGuesses;
do{
randomNumber =random.nextInt(100)+1;
System.out.println ("Enter the guessing number");
input = keyboard.nextInt();
keyboard.nextLine();
// for (i=0; i<input; i++;){
// numberGuesses++;}
if (input<1||input>100){
try {
throw new NumberOutOfRangeException ("Number is not between range"+input);}
catch (InputMismatchException e) {
System.out.println("Your number is not integer");
keyboard.nextLine();
}
catch (NumberOutOfRangeException ex) {
System.out.println("your number is out of range");
System.exit(0);
}}
if (input ==randomNumber){
System.out.println("Congratulations! You guessed the number in guesses"); }
else if (input <randomNumber){
System.out.println("Your guess was too low, the number was "+randomNumber);}
else if ( input>randomNumber){
System.out.println("Your guess was too high, the number was"+randomNumber);
}}
while(input<1 || input>100);{
System.out.println("you input was"+input );
}
}
private static class NumberOutOfRangeException extends Exception {
public NumberOutOfRangeException(String string) {
}
}
}