14 Replies - 2316 Views - Last Post: 19 September 2011 - 08:02 PM Rate Topic: -----

#1 mastapro   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 133
  • Joined: 19-September 11

Class, Interface, or Enum Expected Errors

Posted 19 September 2011 - 07:08 PM

Hi I'm new to java. I'm writing this program that pits the cpu against the user in guessing a random number. The cpu has to be efficient as possible. I'm getting a bunch of errors about "class, interface, or enum expected". What's wrong with the code? Thanks.

import java.io.*;
import java.util.*;

public class Program1f
{
	public static void main (String[] args) throws IOException
	{
		Scanner kbd = new Scanner(System.in);
		
		int lo, hi, userGuess, cpuGuess;
		
		do
		{
			System.out.println("lo?");
			lo = kbd.nextInt();
			System.out.println("hi?");
			hi = kbd.nextInt();
		} while(lo >= hi || lo < 0);
		
		System.out.println("The secret number is between " +lo+ ".." +hi+ "inclusive.");

		Random rand = new Random();
		int randomNum = rand.nextInt(hi - lo + 1) + lo;
		theGame(lo, hi, randomNum);
	}
}
	
private static void theGame( int low, int high, int secret)
{
	do
	{
		System.out.println("Please enter your guess.");
		userGuess = kbd.nextInt();
			
		if (userGuess = secret)
		{
			System.out.println("You win!!");
		} break;
		else if (userGuess > secret)
		{
			System.out.println("User's guess is too HIGH");
		}
		else if (userGuess < secret)
		{
			System.out.println("User's guess is too LOW");
		}
		
		cpuGuess = (high-low)/2;
		
		if (cpuGuess < secret)
		{
			low = cpuGuess;
		}
		else if (cpuGuess > secret)
		{
			high=cpuGuess;
		}
		else if (cpuGuess == secret)
		{
			System.out.println("Cpu wins!!");
		} break;
		
		System.out.println("Cpu Guess: " +cpuGuess+ "");

	} while true
}


This post has been edited by macosxnerd101: 19 September 2011 - 07:09 PM
Reason for edit:: Renamed title to be more descriptive


Is This A Good Question/Topic? 0
  • +

Replies To: Class, Interface, or Enum Expected Errors

#2 macosxnerd101   User is online

  • Games, Graphs, and Auctions
  • member icon




Reputation: 12335
  • View blog
  • Posts: 45,443
  • Joined: 27-December 08

Re: Class, Interface, or Enum Expected Errors

Posted 19 September 2011 - 07:11 PM

At line 26, you close your class definition prematurely. So your theGame() method is outside of your class definition.

In your theGame() method, this line } while true will give you syntax errors, b/c a while loop must have the condition in parentheses. And since it's a do-loop, you have to end it with a semi-colon.
Was This Post Helpful? 1
  • +
  • -

#3 mastapro   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 133
  • Joined: 19-September 11

Re: Class, Interface, or Enum Expected Errors

Posted 19 September 2011 - 07:16 PM

View Postmacosxnerd101, on 19 September 2011 - 07:11 PM, said:

At line 26, you close your class definition prematurely. So your theGame() method is outside of your class definition.

In your theGame() method, this line } while true will give you syntax errors, b/c a while loop must have the condition in parentheses. And since it's a do-loop, you have to end it with a semi-colon.

thank you very much. i fixed the premature class definition close and the while true semicolon. It's still giving me some errors, it says line 38, "else if without an if" but there is an if right before it.
Was This Post Helpful? 0
  • +
  • -

#4 macosxnerd101   User is online

  • Games, Graphs, and Auctions
  • member icon




Reputation: 12335
  • View blog
  • Posts: 45,443
  • Joined: 27-December 08

Re: Class, Interface, or Enum Expected Errors

Posted 19 September 2011 - 07:18 PM

Look at the placement of your break; statement.
 if (userGuess = secret)
36	        {
37	            System.out.println("You win!!");
38	        } break;


The break separates the else if and if statements. Wouldn't it make more sense for the break; statement to be in the if statement?
Was This Post Helpful? 1
  • +
  • -

#5 mastapro   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 133
  • Joined: 19-September 11

Re: Class, Interface, or Enum Expected Errors

Posted 19 September 2011 - 07:21 PM

View Postmacosxnerd101, on 19 September 2011 - 07:18 PM, said:

Look at the placement of your break; statement.
 if (userGuess = secret)
36	        {
37	            System.out.println("You win!!");
38	        } break;


The break separates the else if and if statements. Wouldn't it make more sense for the break; statement to be in the if statement?

when i put the break; inside the if, there's 12 new errors that popped up. when it was outside like i had, there was only 1 error... what could be going on?
Was This Post Helpful? 0
  • +
  • -

#6 macosxnerd101   User is online

  • Games, Graphs, and Auctions
  • member icon




Reputation: 12335
  • View blog
  • Posts: 45,443
  • Joined: 27-December 08

Re: Class, Interface, or Enum Expected Errors

Posted 19 September 2011 - 07:22 PM

Explain why you need the break statement. What is the logic. Also, posting your revised code and errors would be helpful.
Was This Post Helpful? 0
  • +
  • -

#7 mastapro   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 133
  • Joined: 19-September 11

Re: Class, Interface, or Enum Expected Errors

Posted 19 September 2011 - 07:27 PM

View Postmacosxnerd101, on 19 September 2011 - 07:22 PM, said:

Explain why you need the break statement. What is the logic. Also, posting your revised code and errors would be helpful.

the break is for if the user or the cpu gets the right guess, then i would break out of the do loop and finish the program.

import java.io.*;
import java.util.*;

public class Program1f
{
	public static void main (String[] args) throws IOException
	{
		Scanner kbd = new Scanner(System.in);
		
		int lo, hi, userGuess, cpuGuess;
		
		do
		{
			System.out.println("lo?");
			lo = kbd.nextInt();
			System.out.println("hi?");
			hi = kbd.nextInt();
		} while(lo >= hi || lo < 0);
		
		System.out.println("The secret number is between " +lo+ ".." +hi+ "inclusive.");

		Random rand = new Random();
		int randomNum = rand.nextInt(hi - lo + 1) + lo;
		playGame(lo, hi, randomNum);
	}
	
private static void playGame( int low, int high, int secretNum )
{
	do
	{
		System.out.println("Please enter your guess.");
		userGuess = kbd.nextInt();
			
		if (userGuess < secretNum)
		{
			System.out.println("User's guess is too LOW");
		}
		else if (userGuess > secretNum)
		{
			System.out.println("User's guess is too HIGH");
		}
		else if (userGuess == secretNum)
		{
			System.out.println("You win!!");
		} break;
		
		cpuGuess = (high-low)/2;
		
		if (cpuGuess < secretNum)
		{
			low = cpuGuess;
		}
		else if (cpuGuess > secretNum)
		{
			high = cpuGuess;
		}
		else if (cpuGuess == secretNum)
		{
			System.out.println("Cpu wins!!");
		} break;
		
		System.out.println("Cpu Guess: " +cpuGuess+ "");

	} while (true);
}
}


Was This Post Helpful? 0
  • +
  • -

#8 macosxnerd101   User is online

  • Games, Graphs, and Auctions
  • member icon




Reputation: 12335
  • View blog
  • Posts: 45,443
  • Joined: 27-December 08

Re: Class, Interface, or Enum Expected Errors

Posted 19 September 2011 - 07:28 PM

Having the break; statements outside the if statements makes no sense. After this else if statement, your loop will terminate:
 else if (userGuess == secretNum)
43	        {
44	            System.out.println("You win!!");
45	        } break;



So it sounds like you want your break; statement in the appropriate if statement.
Was This Post Helpful? 1
  • +
  • -

#9 mastapro   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 133
  • Joined: 19-September 11

Re: Class, Interface, or Enum Expected Errors

Posted 19 September 2011 - 07:32 PM

View Postmacosxnerd101, on 19 September 2011 - 07:28 PM, said:

Having the break; statements outside the if statements makes no sense. After this else if statement, your loop will terminate:
 else if (userGuess == secretNum)
43	        {
44	            System.out.println("You win!!");
45	        } break;



So it sounds like you want your break; statement in the appropriate if statement.


you're right, i should probably move the break; inside the else if statements. I just fixed that and i'm still getting 12 errors.
all the errors are very similar though,
"symbol: variable cpuGuess
location: class Program1f
Program1f.java:54: cannot find symbol else if (cpuGuess > secretNum))"

basically 11 other errors like that
Was This Post Helpful? 0
  • +
  • -

#10 macosxnerd101   User is online

  • Games, Graphs, and Auctions
  • member icon




Reputation: 12335
  • View blog
  • Posts: 45,443
  • Joined: 27-December 08

Re: Class, Interface, or Enum Expected Errors

Posted 19 September 2011 - 07:33 PM

Post your revised code and all your errors. Please get in this habit when you make a change. It will make things easier for us. :)
Was This Post Helpful? 0
  • +
  • -

#11 mastapro   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 133
  • Joined: 19-September 11

Re: Class, Interface, or Enum Expected Errors

Posted 19 September 2011 - 07:36 PM

View Postmacosxnerd101, on 19 September 2011 - 07:33 PM, said:

Post your revised code and all your errors. Please get in this habit when you make a change. It will make things easier for us. :)

haha ok sorry,here it is. and is there a way i can somehow copy the cmd errors? or do i have to write them by hand?
	
import java.io.*;
import java.util.*;

public class Program1f
{
	public static void main (String[] args) throws IOException
	{
		Scanner kbd = new Scanner(System.in);
		
		int lo, hi, userGuess, cpuGuess;
		
		do
		{
			System.out.println("lo?");
			lo = kbd.nextInt();
			System.out.println("hi?");
			hi = kbd.nextInt();
		} while(lo >= hi || lo < 0);
		
		System.out.println("The secret number is between " +lo+ ".." +hi+ "inclusive.");

		Random rand = new Random();
		int randomNum = rand.nextInt(hi - lo + 1) + lo;
		playGame(lo, hi, randomNum);
	}
	
private static void playGame( int low, int high, int secretNum )
{
	do
	{
		System.out.println("Please enter your guess.");
		userGuess = kbd.nextInt();
			
		if (userGuess < secretNum)
		{
			System.out.println("User's guess is too LOW");
		}
		else if (userGuess > secretNum)
		{
			System.out.println("User's guess is too HIGH");
		}
		else if (userGuess = secretNum)
		{
			System.out.println("You win!!");
			break;
		}
		
		cpuGuess = (high-low)/2;
		System.out.println("Cpu Guess: " +cpuGuess+ "");

		if (cpuGuess < secretNum)
		{
			low = cpuGuess;
		}
		else if (cpuGuess > secretNum)
		{
			high = cpuGuess;
		}
		else if (cpuGuess = secretNum)
		{
			System.out.println("Cpu wins!!");
			break;
		}
	} while (true);
}
}


Was This Post Helpful? 0
  • +
  • -

#12 macosxnerd101   User is online

  • Games, Graphs, and Auctions
  • member icon




Reputation: 12335
  • View blog
  • Posts: 45,443
  • Joined: 27-December 08

Re: Class, Interface, or Enum Expected Errors

Posted 19 September 2011 - 07:38 PM

You didn't declare the cpuGuess variable in your theGame() method, or as a global, so it doesn't exist within that scope. You declared it in main(), making it local to that method and inaccessible in theGame().
Was This Post Helpful? 1
  • +
  • -

#13 mastapro   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 133
  • Joined: 19-September 11

Re: Class, Interface, or Enum Expected Errors

Posted 19 September 2011 - 07:40 PM

View Postmacosxnerd101, on 19 September 2011 - 07:38 PM, said:

You didn't declare the cpuGuess variable in your theGame() method, or as a global, so it doesn't exist within that scope. You declared it in main(), making it local to that method and inaccessible in theGame().

ah ok, so do i just delete the int declaration of cpuGuess in the main, then put "int cpuGuess;" in the playGame method?
Was This Post Helpful? 0
  • +
  • -

#14 mastapro   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 133
  • Joined: 19-September 11

Re: Class, Interface, or Enum Expected Errors

Posted 19 September 2011 - 07:49 PM

actually i just figured it out thanks.. u can delete the topic
Was This Post Helpful? 0
  • +
  • -

#15 macosxnerd101   User is online

  • Games, Graphs, and Auctions
  • member icon




Reputation: 12335
  • View blog
  • Posts: 45,443
  • Joined: 27-December 08

Re: Class, Interface, or Enum Expected Errors

Posted 19 September 2011 - 08:02 PM

Glad you figured it out! We don't delete topics, by the way. :)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1