Java 如何生成字符串int终止符?
所以我做了一个大家都喜欢的游戏“石头,布,剪刀”,我让所有的东西都工作了,除了让while循环在停止前重复3次。它确实重复3次并停止,但第2次和第3次重复变量不变。看看代码,告诉我我做错了什么。 **更新:现在我所有的东西都在工作了,如何让这个“Q”字符串终止循环Java 如何生成字符串int终止符?,java,Java,所以我做了一个大家都喜欢的游戏“石头,布,剪刀”,我让所有的东西都工作了,除了让while循环在停止前重复3次。它确实重复3次并停止,但第2次和第3次重复变量不变。看看代码,告诉我我做错了什么。 **更新:现在我所有的东西都在工作了,如何让这个“Q”字符串终止循环 import java.util.Scanner; import java.util.Random; public class RockPaperScissors { /** * (Insert a brief
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++;
}
}
}
实际上对输入执行任何操作的逻辑——所有那些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
}
这样做:
if (in.hasNextInt())
{
input = in.nextInt();
} else if (in.hasNext("Q")) {
// quit
}
实际上对输入执行任何操作的逻辑——所有那些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
}
这样做:
if (in.hasNextInt())
{
input = in.nextInt();
} else if (in.hasNext("Q")) {
// quit
}
特别是,你的“while”循环,除了打印它之外,不会对赢家做任何事情,因此赢家在整个游戏中保持不变,不管输入是什么。这可能是一个开始“调试”的好地方。特别是,你的“while”循环除了打印它之外,不会对赢家做任何事情,因此赢家在整个游戏中保持不变,不管输入是什么。这可能是开始“调试”的好地方。似乎您想要使用
do-while循环
这将有助于:
do{
compint = gen.nextInt(3) + 1;
System.out.println("Enter Rock(1), Paper(2), Scissors(3) {Q to quit]: ");
int input = in.nextInt();
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("Enter Rock(1), Paper(2), Scissors(3) {Q to quit]: ");
input = in.nextInt();
count++;
}while (count < 3);
do{
compint=gen.nextInt(3)+1;
println(“输入石头(1)、纸(2)、剪刀(3){Q退出]:”;
int input=in.nextInt();
如果(输入=1)
{
usermove=“Rock”;
}
else if(输入=2)
{
usermove=“纸张”;
}
else if(输入=3)
{
usermove=“剪刀”;
}
如果(compint==1)
{
compmove=“岩石”;
}
else if(compint==2)
{
compmove=“纸张”;
}
else if(compint==3)
{
compmove=“剪刀”;
}
如果(compint==输入)
{
获胜者=“平局”;
}
else if(compint==1&&input==3)
{
winner=“计算机”;
}
else if(compint==2&&input==1)
{
winner=“计算机”;
}
else if(compint==3&&input==2)
{
winner=“计算机”;
}
其他的
{
winner=“用户”;
}
系统输出打印(“计算机:+compmove+“|”);
System.out.print(“您:“+usermove+”)”;
System.out.println(“获胜者:+Winner”);
println(“输入石头(1)、纸(2)、剪刀(3){Q退出]:”;
input=in.nextInt();
计数++;
}而(计数<3);
似乎您想要使用do-while循环
这将有助于:
do{
compint = gen.nextInt(3) + 1;
System.out.println("Enter Rock(1), Paper(2), Scissors(3) {Q to quit]: ");
int input = in.nextInt();
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("Enter Rock(1), Paper(2), Scissors(3) {Q to quit]: ");
input = in.nextInt();
count++;
}while (count < 3);
do{
compint=gen.nextInt(3)+1;
println(“输入石头(1)、纸(2)、剪刀(3){Q退出]:”;
int input=in.nextInt();
如果(输入=1)
{
usermove=“Rock”;
}
else if(输入=2)
{
usermove=“纸张”;
}
else if(输入=3)
{
usermove=“剪刀”;
}
如果(compint==1)
{
compmove=“岩石”;
}
else if(compint==2)
{
compmove=“纸张”;
}
else if(compint==3)
{
compmove=“剪刀”;
}
如果(compint==输入)
{
获胜者=“平局”;
}
else if(compint==1&&input==3)
{
winner=“计算机”;
}
else if(compint==2&&input==1)
{
winner=“计算机”;
}
else if(compint==3&&input==2)
{
winner=“计算机”;
}
其他的
{
winner=“用户”;
}
系统输出打印(“计算机:+compmove+“|”);
System.out.print(“您:“+usermove+”)”;
System.out.println(“获胜者:+Winner”);
println(“输入石头(1)、纸(2)、剪刀(3){Q退出]:”;
input=in.nextInt();
计数++;
}而(计数<3);
好的,通过在while循环中添加if语句,我的第一个循环对计算机、用户或获胜者来说都没有答案。第二个和第三个循环工作得很好。我不确定我是否应该知道如何使用“for loops”,但是的,你的示例是正确的,我会说最简单的。如果我不清楚,很抱歉。好的,添加ifwhile循环中的语句我的第一个循环对计算机、用户或获胜者没有答案。第二个和第三个循环效果很好。我不确定我是否知道如何使用“for loops”,但是的,你的示例是正确的,我会说最简单的。如果我不清楚的话,很抱歉。实际上,不,它显示获胜者是计算机ter如果变量落在这些参数中。除1=1(匹配为平局)外,任何其他内容都是用户获胜。实际上,如果变量落在这些参数中,则显示赢家是计算机。除1=1(匹配为平局)外,任何其他内容都是用户获胜。Do-while可能更适合编程,但我确实做到了只使用“while循环”。感谢您修复了我循环中的“compint=gen.nextInt(3)+1;”错误。我确实忽略了这一点,这会使计算机播放器混乱。告诉我如何给您一些积分,我会的。@SkyVar作为积分,您可以对答案进行投票,稍后也可以接受它(如果您发现答案确实解决了您的问题)Do-while可能更适合编程,但我确实让它只使用了“while循环”。感谢您修复了“compint=gen.nex”的错误