首先,你必须知道我是Java的noob,我刚开始编写代码,这是我的第一个编程语言。所以请不要生气,如果我开始有点愚蠢,我只是想学习代码 - 谢谢Java:代码工作不正确
我想做一个简单的“猜测游戏”,但我的代码不等待用户输入。 请帮助我,我不知道该怎么办。
我的代码:
public static void main(String[] args)
{
//Creating the scanner
@SuppressWarnings("resource")
Scanner input = new Scanner(System.in);
//Creating the two random numbers.
Random rand = new Random();
int userNumber = rand.nextInt(10) + 1;
int comNumber = rand.nextInt(10) + 1;
//Asks the user what to do.
System.out.println("Your number is: " + userNumber +" of 10");
System.out.println("Do you think that your number is Heigher(H), Lower(L) or Equal To(E) the computers number");
//Checking if the user is right.
//If the user types in LOWER
if(userNumber < comNumber && input.equals("L"))
System.out.println("You are right. The computer's number is: " + comNumber);
if(userNumber < comNumber && !input.equals("L"))
System.out.println("You are wrong. The computer's number is: " + comNumber);
//If the user types in EQUAL TO.
if(userNumber == comNumber && input.equals("E"))
System.out.println("You are right. The computer's number is: " + comNumber);
if(userNumber == comNumber && !input.equals("E"))
System.out.println("You are wrong. The computer's number is: " + comNumber);
//If the user types in HEIGHER.
if(userNumber > comNumber && input.equals("H"))
System.out.println("You are right. The computer's number is: " + comNumber);
if(userNumber > comNumber && !input.equals("H"))
System.out.println("You are wrong. The computer's number is: " + comNumber);
else
System.out.println("You can only type in ' L ', ' E ' or ' H '.");
}
}
我会很高兴,如果你能帮助我白衣我的问题,并告诉我,我怎么能删除@SuppressWarnings(“资源”)/解释为什么必须在那里。
好吧所以/而不是我应该把scanner.nextLine()?我应该在哪里放置Integer.parseInt?顺便说一句,谢谢你的帮助:) – Kenno
那么,scanner.nextLine()等待用户输入,然后将其作为字符串返回。所以,在你需要使用输入之前,坚持你想要的地方。至于Integer.parseInt(),如果你愿意,你可以把它放在scanner.nextLine()之后,它只是将一个String转换为一个int。 – Kayaman
好吧,我添加了scanner.nextLine()和Integer.parseInt(userInput),现在我的代码/ eclipse不会显示控制台/ run - 我该怎么喊? – Kenno