I'm getting an illgal start of expression error in my code for the public static boolean portion at the bottom... any ideas?

I'm getting an illgal start of expression error in my code for the public static boolean portion at the bottom... any ideas?

heres my code

import java.util.Scanner;
import java.util.Random;
public class numberGame {
    public static void main (String[] args) {
        Random rand = new Random ();
        Scanner keyboard = new Scanner(System.in); //User input
        int x = (int)(100 * Math.random()) + 1; //Generates random number
        int userInt = 0; //user input number
        char userAns;
        int counter = 0;
        boolean guess = true;
        System.out.print("The Number guessing game, Would you like to play?: ");
        String userAns1 = keyboard.nextLine();
        userAns = userAns1.charAt(0); 
        while (guess == true)
        {
            char userAns2 = 'y'; //Compare to Yes or No answer from user
            if (userAns != userAns2)
            {
                System.out.println("Goodbye!");
                guess = false;
                }
                else
                {
                    System.out.println("\nI am thinking of a number between 1 and 100. Try to guess it.\n");
        {
            System.out.println("\nI am thinking of a number between 1 and 100. Try to guess it.\n ");
            while (x != userInt)
            {
                System.out.print("Enter Number ");
                userInt = keyboard.nextInt();
                if (x < userInt)
                {
                    System.out.println(userInt + " Way too high, guess again ");
                    }
                    else if (x > userInt)
                    {
                        System.out.println(userInt + " too small, keep trying ");
                        }
                        else if (x = userInt)
                        {
                            System.out.println(userInt + " Right on the money, DING DING DING!!! ");
                        }
                        counter = counter + 1;                                                  
                        }
                        if (counter <= 3)
                        {
                            System.out.println("You've got it in " + counter + " Great work! You are a mathematical wizard!");
                            break;
                            }
                            else if (counter >= 3 && counter <= 7)
                                {
                                    System.out.println("You've got it in " + counter + "guesses, Not too bad! You've got some potential.");
                                    break;
                                    }
                                    else if (counter > 7)
                                    {
                                        System.out.println("You've got it in " + counter + " guesses. What took you so long? Maybe you should take some lessons");
                                        break;
                                        }
                                        {

public static boolean playAgain(){        
                        String answer;        
                        Scanner keyboard = new Scanner(System.in);
                        System.out.println("would you like to play again? ");        
                        answer = keyboard.nextLine();        
                        if (answer =="y");        
                        return true;    }
                        else   
                        return false;
                    }}}}}}
View Answers

July 3, 2012 at 11:09 AM

Here is a code of number game. We have modified it.

import java.util.Scanner;
import java.util.Random;
public class numberGame {
    public static void main (String[] args) {
        Random rand = new Random (); 
        Scanner keyboard = new Scanner(System.in);
        int x = (int)(100 * Math.random()) + 1;
        int userInt = 0;
        char userAns;
        int counter = 0;
        boolean guess = true;
        System.out.print("The Number guessing game, Would you like to play?: ");
        String userAns1 = keyboard.nextLine(); 
        userAns = userAns1.charAt(0);
        while (guess == true) {
            char userAns2 = 'y';
            if (userAns != userAns2) { 
                System.out.println("Goodbye!");
                guess = false;
            }
            else {
                System.out.println("\nI am thinking of a number between 1 and 100. Try to guess it.\n");

                while (x != userInt) { 
                    System.out.print("Enter Number ");
                    userInt = keyboard.nextInt();
                    if (x < userInt) { 
                        System.out.println(userInt + " Way too high, guess again ");
                    }
                    else if (x > userInt) {
                        System.out.println(userInt + " too small, keep trying "); 
                    }
                    else if (x == userInt) { 
                        System.out.println(userInt + " Right on the money, DING DING DING!!! ");
                    } counter = counter + 1;
                    } 
                    if (counter <= 3) { 
                        System.out.println("You've got it in " + counter + " Great work! You are a mathematical wizard!");
                        break;
                    }
                    else if (counter >= 3 && counter <= 7) { 
                        System.out.println("You've got it in " + counter + "guesses, Not too bad! You've got some potential.");
                        break; 
                    }
                    else if (counter > 7) {
                        System.out.println("You've got it in " + counter + " guesses. What took you so long? Maybe you should take some lessons");
                        break;
                    }
            }
        }
    }
                   public static boolean playAgain(){
                   String answer;
                   Scanner keyboard = new Scanner(System.in); System.out.println("would you like to play again? ");
                   answer = keyboard.nextLine();
                   if (answer =="y"){
                    return true; 
                    } 
                   else{
                    return false; 
                   }
                   }
                   }









Related Tutorials/Questions & Answers:
I'm getting an illgal start of expression error in my code for the public static boolean portion at the bottom... any ideas?
illegal start of expression in servlet error..
Advertisements
why i'm getting this error?The import javax.servlet.http cannot be resolved
illegal start of expression - Java Beginners
getting error in your login form code
i got illegal start of expression error , plese tell me the mistake where i did...
i am unable to identify the error in my code
i am unable to identify the error in my code
ERROR with my JAVA code - Java Beginners
error : not an sql expression statement
Getting an error :(
public static void main
Static keyword in public static void main
Java - Boolean Expression and Operation in Java
Getting Error - Development process
Error in a code. - Java Beginners
Getting error java.lang.ClassNotFoundException: com.mysql.jdbc.Driver.
Applet run with appletviewer but not in browser, any code problem.
java code for print true if even and false if odd and print error if any float value with out using conditional and looping statement
Java error illegal start of type
i cant find any compile time error but there is runtime error.
Code error
ModuleNotFoundError: No module named 'portion'
getting an error when set classpath
Swing error in code
Still getting the http 404 error - Java Beginners
My Base Class is Changing every time in my code. How I can overcome this?
Error in Code - Development process
What happen when we write static public void instead of public static void?
data type used to store name.(getting an error)
Solve my code
getting radio button at start of each row of table - Struts
Understanding public static void main function
Problem in my code - Development process
Finding start and end index of string using Regular expression
I GOT ERROR FOR MY PROJECT
ModuleNotFoundError: No module named 'bottom'
tomcat server start up error - Struts
error in code - JDBC
My Sql Error - Development process
apache tomcat start error - Java Server Faces Questions
Getting connect error in Jmeter - Development process
How can I start my career in data science?
Getting File path error - JSP-Servlet
boolean help
ModuleNotFoundError: No module named 'static-code-checker'
pls review my code - Struts
this is my code java - Date Calendar
My Servlet code not working - Development process
Tomcat start up issue: SEVERE: Error listenerStart - IDE Questions

Ads