java不是一个语句
我正在从一本书中学习用java编写代码。它给出了一个猜谜游戏的例子,并给出了代码。我想把它作为参考。但我总是出错。这可能是我打字的方式,因为我用kindle阅读,结果有点乱
像这样的错误有很多,但没有一个像我这样。我试图做一个猜谜游戏,但我不断得到这个错误:
猜谜游戏。java:17:不是一个声明 (int)(Math.random()*10)+1
代码:
import java.util.Scanner;
public class GuessingGame
{
static Scanner sc = new
Scanner(System.in);
public static void
main(String[] args)
{
bolean keepPlaying = true;
System.out.println("Let's play a guessing game!");
while (keepplaying)
{
bolean validInput;
int number, guess;
String answer;
// Pick a random number =
(int)(Math.random() * 10) + 1;
// Get the guess
System.out.print("What do you think it is? ");
do
{
guess = sc.nextInt();
validInput = true;
if ((guess < 1) || (guess > 10))
{
System.out.print
("I said between 1 and 10. "
+ "Try again: ");
validInput = false;
}
}while (!validInput);
// Check the guess
if (guess == number)
System.out.println(
"You're right!");
else
System.out.println(
"You're wrong! " + "The number was " + number);
// Play again?
do
{
System.out.println("\nPlay again? (Yes or No)");
answer = sc.next();
validInput = true;
if (asnwer.equalsIgnoreCase("Yes"));
else if (answer.egualsIgnoreCase("No")-
keepPlaying = false);
else
validInput = false;
} while (!validInput);
}
System.out.println("\nThank you for playing!");
}
}
# 1 楼答案
没错,
(int)(Math.random() * 10) + 1;
不是一种陈述。与其他语言不同,Java不允许只使用表达式作为语句我认为上面评论中的
number
一词属于这一行:不是:
但是:
(上面已经有了一个
int number;
,所以变量已经被声明了,所有的东西都被声明了。)