0% found this document useful (0 votes)
9 views1 page

Final Project Homework Thing

This Java program allows a user to play a guessing game where they try to guess a randomly generated number between 1-100. The program tracks the number of guesses and provides feedback if the guess is too high or low until the user guesses correctly.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views1 page

Final Project Homework Thing

This Java program allows a user to play a guessing game where they try to guess a randomly generated number between 1-100. The program tracks the number of guesses and provides feedback if the guess is too high or low until the user guesses correctly.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 1

import java.util.

Scanner;
public class Homework
{
public static void main (String[] args)
{
String str, another = "y";
Scanner input = new Scanner (System.in);
int random = (int)((Math.random()*5) + 1);
int guess = 0;
int count = 0;
while (another.equalsIgnoreCase("y"))
{
random = (int)((Math.random()*100) + 1);
System.out.println("The secret number is from 1 to 100 (inclusive)");
System.out.println();
System.out.print("Enter your guess (0 to quit): ");
guess = input.nextInt();
count = 0;
while (guess != 0)
{
if (guess > random)
{

System.out.println("Your guess of " + guess + " was too HIGH.");


System.out.println();
System.out.print("Enter your guess (0 to quit): ");
guess = input.nextInt();
count++;
}
else if (guess < random)
{
System.out.println("Your guess of " + guess + " was too LOW.");
System.out.println();
System.out.print("Enter your guess (0 to quit): ");
guess = input.nextInt();
count++;
}

else if (guess == random)


{
System.out.println("Correct! " + guess + " was what I chose.");
guess = 0;
count++;
System.out.println("It took you " + count + " guesses.");

}
}
System.out.println();
System.out.print ("Play again (y/n)?: ");
another = input.next();
System.out.println();
}
}
}

You might also like