Reputation: 1217
I'm making a number guessing program. A random number is generated, and the user tries to guess it. The program will print "too high" or "too low" and let the user guess again. I'm having an issue inputing guesses after the first one into the method that takes the guess.
Here is my class:
import java.util.Scanner;
public class Lab8
{
public static void main (String [] args)
{
Scanner in = new Scanner(System.in);
System.out.println("Enter a number: ");
MyNumberGuess MyNumberGuess = new MyNumberGuess(in.nextInt());
while (MyNumberGuess.tooLow() == true || MyNumberGuess.tooHigh() == true)
{
if (MyNumberGuess.tooHigh() == true)
{
System.out.println("Too high");
System.out.println("Enter a number: ");
MyNumberGuess.MyNumberGuess(in.nextInt());
}
else if (MyNumberGuess.tooLow() == true)
{
System.out.println("Too low");
System.out.println("Enter a number: ");
MyNumberGuess.MyNumberGuess(in.nextInt());
}
}
System.out.println("Correct");
System.out.println("You made " + MyNumberGuess.getNumGuesses() + " guesses");
}
}
Here's the other class, and the problem method:
import java.util.*;
public class MyNumberGuess
{
public static final int MAX_GUESS = 1000;
private int theNumber, numGuesses, prevGuess;
public MyNumberGuess(int inGuess)
{
Random generator = new Random();
numGuesses = 1;
prevGuess = inGuess;
theNumber = generator.nextInt(MAX_GUESS);
}
}
As is, I'm getting a "cannot find symbol" error when compiling in my first class on this line:
MyNumberGuess.MyNumberGuess(in.nextInt());
I've tried calling it in different ways, using parameters and not, and trying to call the variables alone, thought they are supposed to be private. Any help is appreciated.
Upvotes: 0
Views: 29879
Reputation: 16373
I had a weird issue: no matter what I couldn't access the public methods of one of my classes whether it was static or whether I setup to instantiate it. I ended up deleting the class and creating a new one with a different name. I must have been stepping on a built in class name.
Upvotes: 0
Reputation: 3
You are missing a curly bracket, you have your class definition and then you have a function definition but no ending semicolon. And you shouldn’t use the same name for so many variables.
Upvotes: 0
Reputation: 279890
You previously used
MyNumberGuess MyNumberGuess = new MyNumberGuess(in.nextInt());
to create an instance of your class. Why would you then use
MyNumberGuess.MyNumberGuess(in.nextInt());
to do the same thing?
This
public MyNumberGuess(int inGuess)
{
Random generator = new Random();
numGuesses = 1;
prevGuess = inGuess;
theNumber = generator.nextInt(MAX_GUESS);
}
is a constructor. You need to invoke it with the new
operator.
Just re-initialize your variable
MyNumberGuess = new MyNumberGuess(in.nextInt());
Note that java convention states that variables' names should begin with a lower case character.
On another note, this piece of code
while (MyNumberGuess.tooLow() == true || MyNumberGuess.tooHigh() == true)
is redundant. The method call MyNumberGuess.tooLow()
already returns a true
or false
value, so why compare it with == true
? Just use it directly. For example
if (MyNumberGuess.tooLow()) // read it as "If my number guess is too low"
or
if (!MyNumberGuess.tooLow()) // read it as "If my number guess is not too low"
Apply that appropriately with the while
.
Upvotes: 5
Reputation: 208944
Don't use the exact class name as the variable name
MyNumberGuess MyNumberGuess = new MyNumberGuess(in.nextInt());
Change the casing if anything
MyNumberGuess myNumberGuess = new MyNumberGuess(in.nextInt());
Upvotes: 3