Got that one now for another problem. At the end I want them to either enter an "y" or an "n". Depending on if they want to run the program again. I need help doing that.

Exception in thread "main" java.lang.Error: Unresolved compilation problems:
The local variable y may not have been initialized
The local variable n may not have been initialized
The local variable a may not have been initialized

at Driver.main(Driver.java:10)

import java.util.Scanner;
import java.util.Random;

public class Driver 
{
public static void main(String[] args) 
{
int z;
Double a;
Double y = y;
Double n = n;
Scanner stdIn = new Scanner(System.in);
Random rand = new Random();
int x = rand.nextInt(100)+1;
do
{

System.out.println("Please guess a random number;");
z = stdIn.nextInt();
while(z != x)
{
if(z>x)
{
System.out.println("Too High. Guess Again:");
z = stdIn.nextInt();
}
else if(z<x)
{
System.out.println("Too Low. Guess Again:");
z = stdIn.nextInt();
}
System.out.println("Thats Correct!! To play again press y or n to exit.");
a = stdIn.nextDouble();

}

}while(a == y);

System.out.println("Thanks for playing!");
}

}

You have to initialize your variables when declared in a method before you use them. Do it like this and it should work:

int z = 0;
Double a = 0.0;
Double y = 0.0;
Double z = 0.0;

Or you can use the constructors like this:

Double a = new Double(0.0);

That will clear up those errors.

Or, if you want to use primitives:

int z = 0;
double a = 0.0;
double y = 0.0;
double z = 0.0;

Note that Double is the object wrapper of the primitive double. And Integer is the object wrapper of int. Since Java 5.0, primitives and their object wrappers automatically convert to each other.

This article has been dead for over six months. Start a new discussion instead.