-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathGuessNumberOneTime.java
More file actions
57 lines (48 loc) · 1.21 KB
/
GuessNumberOneTime.java
File metadata and controls
57 lines (48 loc) · 1.21 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
import java.util.Scanner;
public class GuessNumberOneTime
{
public static void main( String[] args )
{
System.out.println("Guess a magic number between 0 and 100");
int random = getRandomNumber();
boolean isCorrect = checkGuess(getInput(), random);
while(isCorrect == false)
{
isCorrect = checkGuess(getInput(), random);
}
result(random);
}
public static int getRandomNumber()
{
int randomNumber = (int)(Math.random() * 100);
return randomNumber;
}
public static int getInput()
{
int randomGuess;
Scanner input = new Scanner(System.in);
System.out.println("Enter your guess: ");
randomGuess = input.nextInt();
return randomGuess;
}
public static boolean checkGuess(int guess, int number)
{
if(guess < number)
{
System.out.println("Too low!");
return false;
}
else if(guess > number)
{
System.out.println("Too high!");
return false;
}
else{
return true;
}
}
public static void result(int number)
{
System.out.println("Yes, the number is " + number);
}
}