gpt4 book ai didi

java - 如何创建 String int 终止符?

转载 作者:行者123 更新时间:2023-12-01 18:54:03 25 4
gpt4 key购买 nike

所以我正在制作每个人都喜欢的有趣游戏“石头、剪刀、布”,除了 while 循环在停止之前重复 3 次之外,我一切正常。嗯,它确实重复了 3 次并停止,但第二次和第三次重复变量没有改变。看一下代码并告诉我我做错了什么。**更新:既然我已经完成了所有工作,我如何获得这个“Q”字符串来终止循环?

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

public class RockPaperScissors
{

/**
* (Insert a brief description that describes the purpose of this method)
*
* @param args
*/
public static void main(String[] args)
{
int compint;
String usermove = "";
String compmove = "";
String winner = "";
int count = 0;


Scanner in = new Scanner(System.in);
Random gen = new Random();

System.out.println("Enter Rock(1), Paper(2), Scissors(3) {Q to quit]: ");
int input = in.nextInt();

while (count < 3)
{
compint = gen.nextInt(3) + 1;

if (input == 1)
{
usermove = "Rock";
}
else if (input == 2)
{
usermove = "Paper";
}
else if (input == 3)
{
usermove = "Scissors";
}

if (compint == 1)
{
compmove = "Rock";
}
else if (compint == 2)
{
compmove = "Paper";
}
else if (compint == 3)
{
compmove = "Scissors";
}

if (compint == input)
{
winner = "TIE";
}
else if (compint == 1 && input == 3)
{
winner = "COMPUTER";
}
else if (compint == 2 && input == 1)
{
winner = "COMPUTER";
}
else if (compint == 3 && input == 2)
{
winner = "COMPUTER";
}
else
{
winner = "USER";
}

System.out.print("Computer: " + compmove + " | ");
System.out.print("You: " + usermove + " | ");
System.out.println("Winner: " + winner);
System.out.println();
System.out.println("Enter Rock(1), Paper(2), Scissors(3) {Q to quit]: ");
input = in.nextInt();
count++;

}
}
}

输出:

Enter Rock(1), Paper(2), Scissors(3) {Q to quit]: 
1
Computer: Scissors | You: Rock | Winner: USER

Enter Rock(1), Paper(2), Scissors(3) {Q to quit]:
2
Computer: Rock | You: Paper | Winner: USER

Enter Rock(1), Paper(2), Scissors(3) {Q to quit]:
Q
Exception in thread "main" java.util.InputMismatchException
at java.util.Scanner.throwFor(Unknown Source)
at java.util.Scanner.next(Unknown Source)
at java.util.Scanner.nextInt(Unknown Source)
at java.util.Scanner.nextInt(Unknown Source)
at RockPaperScissors.main(RockPaperScissors.java:102)

最佳答案

实际上对输入执行任何操作的逻辑 - 所有这些 if 语句 - 都在循环之外。每次迭代查看时,所有逻辑都不会被实际执行。这一切都会先发生。试试这个:

for (int count=0; count < 3; count++)
{
int input = in.nextInt();
int compint = gen.nextInt(3) + 1;

// all the if statements and printing here
}
<小时/>

**UPDATE: Now that I have everything working how do I get this "Q" string to terminate the loop?

您将获得 InputMismatchException当输入 Q 但代码调用 Scanner#nextInt() 时。文档非常清楚问题所在:

Thrown by a Scanner to indicate that the token retrieved does not match the pattern for the expected type, or that the token is out of range for the expected type.

这基本上是扫描器告诉您“您要求一个int,但下一个 token 不是一个”的方式。您可以在 nextInt() 调用之前添加额外的检查,使用 Scanner#hasNextInt() ,验证下一个标记实际上是一个 int。如果它不是 int,那么您可以计划将其作为字符串进行解析。

所以代替这个:

input = in.nextInt();

做这样的事情:

if (in.hasNextInt())
{
input = in.nextInt();
} else if (in.hasNext("Q")) {
// quit
}

关于java - 如何创建 String int 终止符?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14794382/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com