Latest Tutorials| Questions and Answers|Ask Questions?|Site Map



Home Answers Viewqa Java-Beginners I'm getting an illgal start of expression error in my code for the public static boolean portion at the bottom... any ideas?
Login         

View Questions and Answers by Category

Advertisements


 
Have Programming Question? Ask it here!
 
 
 


phil
I'm getting an illgal start of expression error in my code for the public static boolean portion at the bottom... any ideas?
1 Answer(s)      2 years and 4 months ago
Posted in : Java Beginners


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;
                    }}}}}}

Advertisement
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:
Advertisements
 

 

 

DMCA.com