0% found this document useful (0 votes)
27 views2 pages

Game Day

This Java program plays a guessing game where it thinks of a random number between 1 and 50 and the user tries to guess it. The program will generate a random initial guess and ask if it's correct. If wrong, it will ask if the number is higher or lower and narrow the range accordingly. It will continue guessing within the range until it guesses correctly, up to a maximum of 50 guesses.

Uploaded by

superqwerty4
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
27 views2 pages

Game Day

This Java program plays a guessing game where it thinks of a random number between 1 and 50 and the user tries to guess it. The program will generate a random initial guess and ask if it's correct. If wrong, it will ask if the number is higher or lower and narrow the range accordingly. It will continue guessing within the range until it guesses correctly, up to a maximum of 50 guesses.

Uploaded by

superqwerty4
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2

import java.util.

Random;
import java.util.Scanner;


public class compguess {

public static void main(String[] args) throws
InterruptedException {
Scanner input= new Scanner(System.in);
Random gen = new Random();
int winner=0, guess=0, maxguess=50, minguess=1;
String moreless,yesno;

System.out.println("Think of a number between 1 and
50!");
guess = 1+gen.nextInt (50);
Thread.sleep(1500);

while(winner==0){System.out.println("Is your number
"+guess);
System.out.println("Please enter yes or no.");
yesno=input.next();

if
(yesno.equalsIgnoreCase("yes")){System.out.println("YES! I WIN! I
WIN!"); winner=1;}

else{System.out.println("Is your number less than or
greater than "+guess);

System.out.println("Please enter bigger or smaller.");
moreless=input.next();



if
(moreless.equalsIgnoreCase("smaller")){maxguess=guess;
}

else
if(moreless.equalsIgnoreCase("bigger")){minguess=guess+1; }
if (maxguess-
minguess==0){System.out.println("You cheated!"); winner=1;}
if
(winner==0){guess=minguess+gen.nextInt(maxguess-minguess);


if (maxguess-
minguess==1){System.out.println("Your guess must be "+guess);
System.out.println("Please enter yes!");
yesno=input.next();

if
(yesno.equalsIgnoreCase("yes")){System.out.println("YES! I WIN! I
WIN!"); winner=1;}

}
}
}}}
// TODO Auto-generated method stub

}

You might also like