如何在java中创建一个while循环,一旦两个用户输入匹配,它将结束游戏?
我一直在尝试创建一个游戏,要求用户输入两个三个字母的单词;该程序通过将单词分开,并说明字母表中的字母是在彼此之前还是之后,来提供单词匹配的线索 游戏快结束了,但我的问题在一次猜测后尝试开始新的回合时就显现出来了。我需要某种while循环,但我已经多次重新排列代码块,我觉得这让整个过程更加复杂。每次两个输入不完全匹配时,应重复提示第二个用户回答问题以及提示线索 两个用户输入为cat和fan时的输出示例:后、a、前 “after”表示字母在字母表中位于用户字母之后,“before”表示字母在用户字母之前 编辑:我已经尽可能地考虑了答案,非常感谢。到目前为止,我已经实现了一个do-while循环,并试图修复我的变量。最后,我觉得我可能必须创建一个对象,以便在某个条件语句设置为false之后能够重新复制用户输入的代码 我的新问题是 一,。每当我在do while循环中有用户输入时,编译器都找不到符号x1、y1、z1、x2、y2和z2 二,。如果我试图制作一个新的对象,我将重写和重新安排超出可能需要的内容。 这仍在进行中,随着我继续努力,我将不断更新这篇文章 (编辑的代码——我保存了原始代码,可以发送给任何想要查看的人。) 可能会声明3个布尔值(1IsCorrect、2IsCorrect、3IsCorrect)将其默认值设置为false,并在打印值后的if(comp/comp1/comp2==0)语句中将相应的布尔值设置为true。 然后将其放入一段时间(!1IsCorrect | | |!2IsCorrect | |!3IsCorrect) 为isFirstRun before循环声明一个变量,并在声明时将其实例化为true,然后在最后设置为false。 做一个假设!isFirstRun语句,并在那里添加代码以询问下一个猜测如何在java中创建一个while循环,一旦两个用户输入匹配,它将结束游戏?,java,while-loop,user-input,Java,While Loop,User Input,我一直在尝试创建一个游戏,要求用户输入两个三个字母的单词;该程序通过将单词分开,并说明字母表中的字母是在彼此之前还是之后,来提供单词匹配的线索 游戏快结束了,但我的问题在一次猜测后尝试开始新的回合时就显现出来了。我需要某种while循环,但我已经多次重新排列代码块,我觉得这让整个过程更加复杂。每次两个输入不完全匹配时,应重复提示第二个用户回答问题以及提示线索 两个用户输入为cat和fan时的输出示例:后、a、前 “after”表示字母在字母表中位于用户字母之后,“before”表示字母在用户字母
希望这能起作用一些与您的问题无关的事情-您不需要两个扫描仪同时读取同一个输入流,而且
var
是Java 10+中的一个关键字,因此您可能需要一个更具描述性的名称。至于循环本身,我想你可以有一些布尔值相同的,最初设置为false,只有当两个输入相等时才设置为true。然后你做do{/*请求输入*/},(!相同)
谢谢!我不知道我可以同时使用一台扫描仪,这就简化了很多事情。即使我决定不使用这个变量,我也一定要改变它,我会注意到var是将来的一个关键字。这很有帮助!我以前从未想过使用do-while循环,所以我确保先这样做。我可能会尝试为代码创建一个对象,这样就不必在最后复制它,但我已经有点超出了我自己的深度,这篇文章对我来说是一个学习过程。接下来我将尝试以某种方式实现isFirstRun语句。这太棒了!使用isFirstRun,您可以使用它不断地只让玩家2尝试猜测,直到他们猜对为止,这可以为打印前后添加一些功能。还可以练习命名变量,例如x1 y1等可以是P1Letter1和P2Letter1等(避免使用x/y/z),您可以将比较语句插入if语句,这样就不需要comp变量。也可常规校准扫描仪或仅为清晰起见进行扫描
import java.util.Scanner;
class Main{
public static void main(String[] args) {
System.out.println("Guess The Word\n(requires two players)");
System.out.println("If the letter in each word matches, the letter will be reprinted.");
System.out.println("If the letter guessed doesn't match, either \"before\" or \"after\" will print.");
Scanner in1=new Scanner(System.in);
//Scanner in2=new Scanner(System.in);
try{
boolean correct1=false; boolean correct2=false; boolean correct3=false;
int indication=0;
//asks for user input and stores in substrings
do{
System.out.print("First Player, enter a three letter word: ");
String user1=in1.nextLine();
String x1=user1.substring(0,1);
String y1=user1.substring(1,2);
String z1=user1.substring(2,3);
System.out.print("Second Player, enter a three letter word: ");
String user2=in1.nextLine();
String x2=user2.substring(0,1);
String y2=user2.substring(1,2);
String z2=user2.substring(2,3);
}
while(!correct1||!correct2||!correct3);
//possible end to loop
// if(user1==user2){indication=1;}
// else{indication=0;}
//comparisons of each letter
int comp;
int comp2;
int comp3;
comp=x1.compareTo(x2);
comp2=y1.compareTo(y2);
comp3=z1.compareTo(z2);
//while1=(comp!=0);
//while2=(comp2!=0);
//while3=(comp3!=0);
//if statement 1
if(comp==0){
System.out.print(x1);
correct1=true;
}
else{
// System.out.println(comp);
if(comp>0){
System.out.println("before, ");
}
else{
System.out.print("after, ");
}
}
//if statement 2
if(comp2==0){
System.out.print(y1);
correct2=true;
}
else{
// System.out.println(comp);
if(comp2>0){
System.out.println(", before, ");
}
else{
System.out.print(", after, ");
}
}
//if statement 3
if(comp3==0){
System.out.print(z1);
correct3=true;
}
else{
// System.out.println(comp3);
if(comp3>0){
System.out.println(", before");
}
else{
System.out.print(", after");
}
}
//ignore else{System.out.print("Congratulations!");}
}
finally{in1.close();}/* in2.close();}*/
}
}