I am having a huge problem creating a short program for my course, I have referenced youtube videos with no help at all, in fact I have used a video in the code below, that worked for the user, but did not work for me, it takes the input and shuts down. The application should ask the user for a randomly generated number between 1-100, at the guess of the user, it should tell them if the answer is high or low and whether they would like to try again or quit, at finding the correct number, which is very hard, the application should tell them the correct number and how many attempts the user made.
So I understand that this requires more than 1 while loop but even in my course work, there are not many examples to use for reference.
I should also mention that I work and learn independently, so I cant just ask the prof for help.... I think that is a last resort.
So in the past this website has helped me tons, and....again hopefully someone can assist, thanks again guys for any help.
P.S. the code below was copied from a youtube video, where it worked, but when I tried it,.... no luck.
import java.util.*;
import java.util.Scanner;
public class hi_low_game {
public static void main (String[] args) {
final int MAX = 100;
int answer, guess, Random;
Random generator = new Random();
Scanner scan = new Scanner (System.in);
answer=generator.nextInt(MAX) + 1;
guess=scan.nextInt();
while ((guess > 100) || (guess <= 0))
{
System.out.println("Guess a number between 1 & 100");
if ((guess > 100) || (guess <= 0))
{
System.out.println("Your number " + guess + "
is not correct. Try Again?");
}
else
{
System.out.println("Your choice was correct !!
The number was: " + answer);
}
}
}
}
Formatted code:
import java.util.*;
import java.util.Scanner;
public class hi_low_game {
public static void main(String[] args) {
final int MAX = 100;
int answer, guess, Random;
Random generator = new Random();
Scanner scan = new Scanner(System.in);
answer = generator.nextInt(MAX) + 1;
guess = scan.nextInt();
while ((guess > 100) || (guess <= 0)) {
System.out.println("Guess a number between 1 & 100");
if ((guess > 100) || (guess <= 0)) {
System.out.println("Your number " + guess + "is not correct.Try Again ? ");
}
else {
System.out.println("Your choice was correct !! The number was: " + answer);
}
}
}
}