-3

我试图显示用户猜测的次数,这些是错误

NumberGame.java:44: 不是语句 System.out.println{"猜测次数:" +guessCount); ^ NumberGame.java:44: ';' 预期 System.out.println{"猜测次数:" +guessCount); ^ NumberGame.java:44: 不是声明 System.out.println{"猜测次数:" +guessCount); ^ NumberGame.java:44: ';' 预期 System.out.println{"猜测次数:" +guessCount); ^ NumberGame.java:48: 解析时到达文件末尾}

我的代码是:

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

public class NumberGame
{
    public static void main(String[] args)
    {
        final int MAX_NUMER = 100;

      int number = rand.nextInt(MAX_NUMBER) + 1;
        int on_off = 1;
      int guessCount = 0;
      int guess; 


      Random rand = new Random();
        Scanner keyboard = new Scanner(System.in);

        while(on_off == 1)
      {
            System.out.print("Guess a number between 1 and 100: ");
         guess = keyboard.nextInt();
         guessCount++;

            if(guess < number)
         {

            System.out.println("Too Low");
            guessCount++;
            }

            if(guess > number)
         {

            System.out.println("Too High");
            guessCount++;
            }

            if(guess == number)
         {

            System.out.println("Correct! You win!");
            }

        }
         System.out.println{"Number of guesses: " + guessCount};

    }
}

任何帮助将不胜感激!谢谢你的时间!

4

5 回答 5

2
 System.out.println{"Number of guesses: " + guessCount);

should be

 System.out.println("Number of guesses: " + guessCount);

You have a typo there. you wrote { instead of (

And in the line

 int number = rand.nextInt(MAX_NUMBER) + 1;

You haven't declared rand

And

int number = rand.nextInt(MAX_NUMBER) + 1;

should be

 int number = rand.nextInt(MAX_NUMER) + 1; //you decalred final int MAX_NUMER 

PS :I'm suggesting you to use IDE,which saves you a lot of time.

于 2013-09-20T06:19:15.080 回答
2

You have a syntax error. Look at your line carefully. You have a { instead of a (.

System.out.println{"Number of guesses: " + guessCount);

You have mismatched brackets.

Further explanation based on your comment:

Curly braces {} usually signify a code block, something you can do to group code as a sort of singular statement, in a very simplified sense (i.e. if (x==1) {statements;} else { more statements;}).

Parentheses () can be used as part of functions. In your case, System.out.println is a function that is given to you to use. Inside the (), you pass arguments/parameters. In this case, you are passing the String you want Java to print out. For your System.out.println(), you want parentheses, not curly braces.

于 2013-09-20T06:19:26.773 回答
2

您的代码中有一个错字:

System.out.println{"Number of guesses: " + guessCount);

左括号是错误的。它应该是:

System.out.println("Number of guesses: " + guessCount);

变量名中也有错字:

final int MAX_NUMER = 100;

它应该是

final int MAX_NUMBER = 100;

希望这可以帮助

于 2013-09-20T06:23:45.083 回答
1

Use ( indstead of { brace in the println

于 2013-09-20T06:19:43.423 回答
1

您更正的语法似乎已将右括号 , 替换为)右大括号}.

在这里,我用第44 行中{的 a替换了。此外,我在第 8 行中修复了一个错字,您在其中写的不是. 此外,需要在第一次使用之前声明和初始化,所以我将它移到了分配值的上方。(MAX_NUMERMAX_NUMBERrandnumber

对于第一个错误,方法调用使用括号,而不是花括号;花括号用于代码块,例如类中的代码,或用于方法体。

第二个错误只是一个错字,而且相当无趣。

没有声明和初始化 rand 的第三个错误就是这样;您需要先声明一个变量才能使用它,而对于 Random 它需要在首次使用之前对其进行初始化。

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

public class NumberGame
{
    public static void main(String[] args)
    {
        final int MAX_NUMBER = 100;

        Random rand = new Random();

        int number = rand.nextInt(MAX_NUMBER) + 1;
        int on_off = 1;
        int guessCount = 0;
        int guess; 

        Scanner keyboard = new Scanner(System.in);

        while(on_off == 1)
        {
            System.out.print("Guess a number between 1 and 100: ");
            guess = keyboard.nextInt();
            guessCount++;

            if(guess < number)
            {
                System.out.println("Too Low");
                guessCount++;
            }

            if(guess > number)
            {
                System.out.println("Too High");
                guessCount++;
            }

            if(guess == number)
            {
                System.out.println("Correct! You win!");
            }
            System.out.println("Number of guesses: " + guessCount);
        }
    }
}
于 2013-09-20T06:27:20.110 回答