Greetings from a new Java coder.

I have ran unto a problem that i cant seem too solve on my own.
And since next class is on Tuesday i hope you can help me out.

When i compile this i get an error on "Line 57 : "Scan cannot be resolved""

And i am wondering why thats the case.

Well cheers for taking your time

//Martin

import java.util.Scanner;
import java.util.Random;
import java.io.*;

public class Game {
    
    
    public static void main(String[] args) {
        
        int tries = 0;
        Random random = new Random();
        boolean playAgain = true;
        
        Scanner input = new Scanner(System.in);
        System.out.println("Guess a number between 0 and 1000");
        
        while (playAgain)
        {
            
            boolean okinput;
            int number = random.nextInt(1000) + 1;
            String answer;
            int guess = -1;
            
        //Ask the user to guess the number
            System.out.println("\nEnter your guess: ");
            guess = input.nextInt();
            okinput = true;
        
            if (guess < 0 || guess > 1000) {
                System.out.println("Try between 0 and 1000 instead");
                okinput = false;
            } 
            
            while (!okinput);
            {
            if (guess > number) {
                System.out.println("Your guess is too high");
            tries++; 
            }
            else if (guess < number) {
                System.out.println("Your guess is too low");
            tries++;
            }
            else {
                System.out.println("Correct!");
            tries++;
            }
            // ending loop    
            
            while (guess == number) {
            System.out.println("You guess the correct answer in " + tries + " tries");    
            break;
            
            do { System.out.print("\nPlay again? (Y or N)" );
            
            answer = scan.next();
            okinput = true;
            
            if (answer.equalsIgnoreCase("Y"));
            else if (answer.equalsIgnoreCase("N"))
                playAgain = false;
                else 
                    okinput = false;
            } while (!okinput);
            System.out.println("\nThank you for playing!");
                    
            
            }    
         }
      }
   }
}

Recommended Answers

All 4 Replies

Where is the variable: scan defined? The compile can not find it.

There is a variable: input that points to a Scanner object. Is that it?

Hi

thanks for replying.

I have been trying to, well basically too understand what you said there.
And i guess i need to define scan or point input to answer?
Don't really sure how to do either at the moment tho..

If you got another bone to throw out here it would probably be great.

You define: Scanner input = new Scanner(System.in);
You use this to read input from the user: guess = input.nextInt();
But then for the next input you have: answer = scan.next();
It looks like you have forgotten that the scanner is called "input" and have mistakenly typed (or copied?) the name "scan" instead, which is wrong.

if (answer.equalsIgnoreCase("Y"));

I'm pretty sure you DON'T have a semicolon at the end of the line there. Because of the semicolon, it skips the rest of the IF STATEMENT

else if (answer.equalsIgnoreCase("N"))
                playAgain = false;
                else 
                    okinput = false;
            }

Remove the semicolon

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.