-2

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);
      }

     }
    }
   }
Dave Jarvis
  • 30,436
  • 41
  • 178
  • 315
cpdl4485
  • 3
  • 2
  • 1
    Format your code. https://codebeautify.org/javaviewer – Dave Jarvis Jul 31 '18 at 17:41
  • 1
    Do not split strings across new lines. Keep them on a single line, as I've shown in the formatted code. Also, provide the error message you see when trying to compile or run the program, if any. – Dave Jarvis Jul 31 '18 at 17:43
  • Your guess = scan line should be inside the while loop, so that the user can guess something new during each iteration. You should also break the loop once the answer is correct. Lastly, a very important rule of software: never trust the user. You should output an error if they guess something invalid. Once you have something running test your edge cases (guess 0 and 100) – LINEMAN78 Jul 31 '18 at 17:53
  • first, you would want to use a running total to keep track of the number of guesses the user makes. second, since you store the user's answer as a variable, you could then compare the real answer against the user answer in an `if` statement. Also, put `scan.nextInt()` inside the loop to keep asking the user for input. Lastly, I don't think you need another `while` loop in this program. – user3170251 Jul 31 '18 at 17:54
  • if the video has the code with that formatting I would advise to try another source... and not so sure if a video is the best media for learning to program.(legal: shouldn't you at least post the link from where the code was copied?) – user85421 Jul 31 '18 at 17:56

1 Answers1

0

To decide whether the guess is correct you should compare with answer. The guess should be read from System.in every time you cycle. And last thing rename the class as HiLowGame.