// ---------1---------2---------3---------4---------5---------6---------7
// 1234567890123456789012345678901234567890123456789012345678901234567890
import java.util.Scanner;
import java.util.Random;
public class RandomNumbersGame
{
public static void main(String[] args)
{
int low = 1;
int max = 100;
int randomNumber = random.nextInt(max - low + 1) + low;
Random rand = new Random();
return randomNumber;
String guessCount = 1;
int userNumber;
Scanner keyboard = new Scanner(System.in);
userNumber = keyboard.nextInt();
while (userNumber != randomNumber)
if (userNumber > randomNumber)
{
System.out.println("Too high, try again.");
}
else (userNumber < randomNumber)
{
System.out.println("Too low, guess again.");
}
else (userNumber == randomNumber)
{
System.out.println("It took you " + "of guesses.");
}
}
}
答案 0 :(得分:0)
首先,您必须跟踪您的开/关括号。此外,您必须将readKey功能置于的while循环中,否则您将一遍又一遍地检查相同的值。尝试像
这样的东西while(userNumber != randomNumber) {
userNumber = keyboard.nextInt();
if(userNumber > randomNumber) {
}
else if(userNumber < randomNumber) {
}
else if(userNumber == randomNumber) {
}
else {
}
}
如果要检查多个条件,则必须使用if语句语法。
此外,您应该阅读方法签名和返回语句,因为
return randomNumber;
根本没有意义。
最后一点是关于数据类型:
String guessCount = 1;
应该是:
int guessCount = 1;
如果您想在之后打印,只需将其转换为string
答案 1 :(得分:0)
public static void main(String... args) {
int low = 1;
int max = 10;
Random random = new Random();
int randomNumber = random.nextInt(max - low + 1) + low;
int guessCount = 1;
int userNumber;
Scanner scanner = new Scanner(System.in);
do {
System.out.println("Enter your guess number = ");
userNumber = scanner.nextInt();
if (userNumber > randomNumber) {
guessCount++;
System.out.println("Too high, try again.");
}
else if (userNumber < randomNumber) {
guessCount++;
System.out.println("Too low, guess again.");
}
else if (userNumber == randomNumber) {
System.out.println("It took you " + guessCount + " times of guesses.");
}
} while (userNumber != randomNumber);
}
我想你想测试如上。 100对于猜测来说太多了:D
答案 2 :(得分:0)
有很多问题:
1&GT; return语句对public static void main(String[] args)
无效。返回类型无效。我不明白你要用Random
来解决什么问题。所以无法修复。它也有多个问题
2 - ;你试图在其他条件之后。这是不正确的。如果您想检查,可以通过多个if
语句进行检查。
if (userNumber > randomNumber)
{
System.out.println("Too high, try again.");
}
if (userNumber < randomNumber)
{
System.out.println("Too low, guess again.");
}
if (userNumber == randomNumber)
{
System.out.println("It took you " + "of guesses.");
}
3&GT; String guessCount = 1;
guessCount是字符串,您尝试分配整数。这是非法的。