repl.it
@egehanasal/

Find the Number (Game)

Java

Game wants you to find the number (which the game chooses) between 1 and 100

fork
loading
Files
  • Main.java
  • jdt.ls-java-project
Main.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
import java.util.Random;

import java.util.Scanner;

public class Main {
  
	
	public static void main(String[] args){
	
		guessNumber();
	}
	
	public static void guessNumber(){
		
		Random random = new Random();
		int x = random.nextInt(100)+1;	// between 1 and 100
		
		Scanner scanner = new Scanner(System.in);
		System.out.println("Enter your guess: ");
		int a = scanner.nextInt();
		int counter = 1;
		
		while (a != x) {
			if (a < x / 2) {
				System.out.println("Too Low. Enter a very higher number: ");
				a = scanner.nextInt();
				counter++;
			}
			else if (a < x) {
				System.out.println("Low. Enter a higher number: ");
				a = scanner.nextInt();
				counter++;
			} else if (a > x * 2) {
				System.out.println("Too high. Enter a very lower number: ");
				a = scanner.nextInt();
				counter++;
			} else if (a > x) {
				System.out.println("High. Enter a lower number: ");
				a = scanner.nextInt();
				counter++;
			}
		}
		if (a == x) {
			System.out.println("CORRECT \nNumber of trials: " + counter);
		}
	}
}
Fetching token
?