Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

fishcrackers

macrumors newbie
Original poster
Jan 22, 2008
8
0
I am having a problem with my program and I need some help. Anything would be greatly appreciated. I am running a guessing game.

Syntax error, insert "while ( Expression ) ;" to complete DoStatement
Syntax error, insert "}" to complete MethodBody


Here is my code:

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

public class Driver
{
public static void main(String[] args)
{
int z;
Double answer;
Double y;
Double n;
Scanner stdIn;
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!! Play again y or n?");
answer = stdIn.nextDouble();

}

}while(answer == y);

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

}
 

Aranince

macrumors 65816
Apr 18, 2007
1,104
0
California
Yay for indenting. Use [ code ] [ /code ] tags next time.

Your else if...the end bracket is { instead of }

Code:
public class Driver
{
    public static void main(String[] args)
    {
        int z;
        Double answer;
        Double y;
        Double n;
        Scanner stdIn;
        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!! Play again y or n?");
                 answer = stdIn.nextDouble();

            }

     }while(answer == y);

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

}
 

fishcrackers

macrumors newbie
Original poster
Jan 22, 2008
8
0
Thanks for the help with that now I have another problem.

The constructor Scanner() is undefined

[ code ]
import java.util.Scanner;
import java.util.Random;

public class Driver
{
public static void main(String[] args)
{
int z;
Double answer;
Double y;
Double n;
Scanner stdIn = new Scanner();
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!! Play again y or n?");
answer = stdIn.nextDouble();

}

}while(answer == y);

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

}
[ /code ]
 

fishcrackers

macrumors newbie
Original poster
Jan 22, 2008
8
0
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)


Code:
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!");
	 }
	   
}[/code}
 

fishcrackers

macrumors newbie
Original poster
Jan 22, 2008
8
0
Help Plz - DoWhile loops

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)

Code:
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!");
}

}
 

markild

macrumors member
Jan 19, 2008
34
0
Oslo, Norway
You should definitly use String or char to store the y/n values (char would probably be easier, seeing it's simple to make comparisons.).

Also, there's no need to store the y and n values, seeing as you only compare them one time in your code, so:
Code:
a == 'y'
should probably do.

I might be a good idea to take a look at some documentation about types, btw.
 

fishcrackers

macrumors newbie
Original poster
Jan 22, 2008
8
0
Thanks, that helped make it run but when I got to run the program again or end it i get this error message.

Exception in thread "main" java.util.InputMismatchException
at java.util.Scanner.throwFor(Unknown Source)
at java.util.Scanner.next(Unknown Source)
at java.util.Scanner.nextDouble(Unknown Source)
at Driver.main(Driver.java:34)
 

fishcrackers

macrumors newbie
Original poster
Jan 22, 2008
8
0
Plz help doWhile loop question

I need help making my program repeat and or end. I give the user the option to run the program again or to end it. When I in a y for yes or n for n. I get this error message.

Exception in thread "main" java.util.InputMismatchException
at java.util.Scanner.throwFor(Unknown Source)
at java.util.Scanner.next(Unknown Source)
at java.util.Scanner.nextDouble(Unknown Source)
at Driver.main(Driver.java:34)


Code:
import java.util.Scanner;
import java.util.Random;

public class Driver 
{
	public static void main(String[] args) 
	{
		int z;
		Double a;
		Double y;
		Double 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!");
	 }
	   
}
 

HiRez

macrumors 603
Jan 6, 2004
6,265
2,630
Western US
It's been a long time since I've used Java, but what is this code supposed to be doing?

Code:
Double y = y;
Double n = n;
It looks like you're assigning something to itself (when it doesn't exist yet). Do you want to assign the integer value of the characters 'y' and 'n'? Why are you using a Double type? Maybe I don't understand it but that assignment code seems fishy to me.
 

eddietr

macrumors 6502a
Oct 29, 2006
807
0
Virginia
Is this your third thread on the same little program? :confused:

I don't see any reason for the following:

Double y
Double n

You don't need those two references, they don't seem to used anywhere.

Then the next issue is why are you reading the response to your question as a double?

and lastly you have a problem in the conditional ('a' == 'y'). That is comparing the literal 'a' against a literal 'y', which will always be false.

What you probably meant was (a == 'y'). But you really probably want (a.equals("y")) with 'a' as a String.

Or use an InputStreamReader instead to read a single char.
 

fishcrackers

macrumors newbie
Original poster
Jan 22, 2008
8
0
Help plz one more time

I can enter 1 or 2 to end the program but it always ends the program and goes with the last "thanks for playing" I cannot get it to repeat the program

Code:
import java.util.Scanner;
import java.util.Random;

public class Driver 
{
	public static void main(String[] args) 
	{
		
		int a;
		int y = 1;
		int z = 0;
		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 1 or 2 to exit.");
		    a = stdIn.nextInt();
	    	
	    }while(a == 'y');
	    
		
	    System.out.println("Thanks for playing!");
	 }
	   
}
 

gnasher729

Suspended
Nov 25, 2005
17,980
5,566
I think at some point you should really try to figure out a few things yourself. But could you please answer the following questions:

1. What is the difference between

a

and

'a'

and

"A"

2. What is the meaning of double and what is the meaning of Double?

If you can answer these questions, then I think you will get a lot lot further, and you don't need to do "programming by guessing" anymore.
 

eddietr

macrumors 6502a
Oct 29, 2006
807
0
Virginia
Dude, no one is going spend the time to chase down every thread you're creating to help you.

Stick to one thread, please. So people can get the context of what you're trying to do. Plus at this rate you'll have 20 threads created tonight.

The reason your program always ends is because the conditional is still (a='y') but now you've switched to ints (1 or 2).

You need (a=1) or (a=2). It's not clear whether you want the user to type 1 or 2 to continue.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.