JAVA BEGINNER PROGRAM JAVA BEGINNER PROGRAM Write a guessing game to guess a num
ID: 3676077 • Letter: J
Question
JAVA BEGINNER PROGRAM
JAVA BEGINNER PROGRAM
Write a guessing game to guess a number between 1 and 1000 (not 100) using method, loops and decision making statements for full credit. A sample program is...
import java.util.Scanner;
public class Guessing {
public static void main(String[] args) {
int low=1, high=100, guess=-1, count=0, avg=0;
Scanner inp = new Scanner(System.in);
System.out.printf("Think of a number between 1 and 100. I will guess seven or less tries. ");
//1st guess
avg=(low+high)/2;
System.out.printf("Hey - Is the number - %2d ",avg);
count++;
System.out.printf("Enter Enter 0 if guessed correctly, 1 for lower and 2 for higher. ");
guess = inp.nextInt();
if (guess==0)
{
System.out.printf("I guess the number in %d times. ", count);
System.exit(1);
}
else
if (guess==1)
high=avg-1;
else
if (guess == 2)
low = avg + 1;
//2nd guess
avg=(low+high)/2;
System.out.printf("Hey - Is the number - %2d ",avg);
count++;
System.out.printf("Enter Enter 0 if guessed correctly, 1 for lower and 2 for higher.");
guess = inp.nextInt();
if (guess==0)
{
System.out.printf("I guess the number in %d times. ", count);
System.exit(1);
}
else
if (guess==1)
high=avg-1;
else
if (guess == 2)
low = avg + 1;
//3rd guess
avg=(low+high)/2;
System.out.printf("Hey - Is the number - %2d ",avg);
count++;
System.out.printf("Enter Enter 0 if guessed correctly, 1 for lower and 2 for higher.");
guess = inp.nextInt();
if (guess==0)
{
System.out.printf("I guess the number in %d times. ", count);
System.exit(1);
}
else
if (guess==1)
high=avg-1;
else
if (guess == 2)
low = avg + 1;
//4th guess
avg=(low+high)/2;
System.out.printf("Hey - Is the number - %2d ",avg);
count++;
System.out.printf("Enter Enter 0 if guessed correctly, 1 for lower and 2 for higher.");
guess = inp.nextInt();
if (guess==0)
{
System.out.printf("I guess the number in %d times ", count);
System.exit(1);
}
else
if (guess==1)
high=avg-1;
else
if (guess == 2)
low = avg + 1;
//5th guess
avg=(low+high)/2;
System.out.printf("Hey - Is the number - %2d ",avg);
count++;
System.out.printf("Enter Enter 0 if guessed correctly, 1 for lower and 2 for higher.");
guess = inp.nextInt();
if (guess==0)
{
System.out.printf("I guess the number in %d times ", count);
System.exit(1);
}
else
if (guess==1)
high=avg-1;
else
if (guess == 2)
low = avg + 1;
//6th guess
avg=(low+high)/2;
System.out.printf("Hey - Is the number - %2d ",avg);
count++;
System.out.printf("Enter Enter 0 if guessed correctly, 1 for lower and 2 for higher.");
guess = inp.nextInt();
if (guess==0)
{
System.out.printf("I guess the number in %d times ", count);
System.exit(1);
}
else
if (guess==1)
high=avg-1;
else
if (guess == 2)
low = avg + 1;
//7th guess
avg=(low+high)/2;
System.out.printf("Hey - Is the number - %2d ",avg);
count++;
System.out.printf("Enter Enter 0 if guessed correctly, 1 for lower and 2 for higher.");
guess = inp.nextInt();
if (guess==0)
{
System.out.printf("I guess the number in %d times ", count);
System.exit(1);
}
else
if (guess==1)
high=avg-1;
else
if (guess == 2)
low = avg + 1;
}
}
Explanation / Answer
I just put high values as 1000 and add the do while loop to guess the value at any number of time .
package guessing;
import java.util.Scanner;
public class Guessing {
public static void main(String[] args) {
int low=1, high=1000, guess=-1, count=0, avg=0;
Scanner inp = new Scanner(System.in);
System.out.printf("Think of a number between 1 and 100. I will guess seven or less tries. ");
//1st guess
do{
avg=(low+high)/2;
System.out.printf("Hey - Is the number - %2d ",avg);
count++;
System.out.printf("Enter Enter 0 if guessed correctly, 1 for lower and 2 for higher. ");
guess = inp.nextInt();
if (guess==0)
{
System.out.printf("I guess the number in %d times. ", count);
System.exit(1);
}
else
if (guess==1)
high=avg-1;
else
if (guess == 2)
low = avg + 1;
//2nd guess
avg=(low+high)/2;
System.out.printf("Hey - Is the number - %2d ",avg);
count++;
System.out.printf("Enter Enter 0 if guessed correctly, 1 for lower and 2 for higher.");
guess = inp.nextInt();
if (guess==0)
{
System.out.printf("I guess the number in %d times. ", count);
System.exit(1);
}
else
if (guess==1)
high=avg-1;
else
if (guess == 2)
low = avg + 1;
//3rd guess
avg=(low+high)/2;
System.out.printf("Hey - Is the number - %2d ",avg);
count++;
System.out.printf("Enter Enter 0 if guessed correctly, 1 for lower and 2 for higher.");
guess = inp.nextInt();
if (guess==0)
{
System.out.printf("I guess the number in %d times. ", count);
System.exit(1);
}
else
if (guess==1)
high=avg-1;
else
if (guess == 2)
low = avg + 1;
//4th guess
avg=(low+high)/2;
System.out.printf("Hey - Is the number - %2d ",avg);
count++;
System.out.printf("Enter Enter 0 if guessed correctly, 1 for lower and 2 for higher.");
guess = inp.nextInt();
if (guess==0)
{
System.out.printf("I guess the number in %d times ", count);
System.exit(1);
}
else
if (guess==1)
high=avg-1;
else
if (guess == 2)
low = avg + 1;
//5th guess
avg=(low+high)/2;
System.out.printf("Hey - Is the number - %2d ",avg);
count++;
System.out.printf("Enter Enter 0 if guessed correctly, 1 for lower and 2 for higher.");
guess = inp.nextInt();
if (guess==0)
{
System.out.printf("I guess the number in %d times ", count);
System.exit(1);
}
else
if (guess==1)
high=avg-1;
else
if (guess == 2)
low = avg + 1;
//6th guess
avg=(low+high)/2;
System.out.printf("Hey - Is the number - %2d ",avg);
count++;
System.out.printf("Enter Enter 0 if guessed correctly, 1 for lower and 2 for higher.");
guess = inp.nextInt();
if (guess==0)
{
System.out.printf("I guess the number in %d times ", count);
System.exit(1);
}
else
if (guess==1)
high=avg-1;
else
if (guess == 2)
low = avg + 1;
//7th guess
avg=(low+high)/2;
System.out.printf("Hey - Is the number - %2d ",avg);
count++;
System.out.printf("Enter Enter 0 if guessed correctly, 1 for lower and 2 for higher.");
guess = inp.nextInt();
if (guess==0)
{
System.out.printf("I guess the number in %d times ", count);
System.exit(1);
}
else
if (guess==1)
high=avg-1;
else
if (guess == 2)
low = avg + 1;
}while(guess > 0);
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.