如何在 java 中创建一个 while 循环,以便在两个用户输入匹配时结束游戏?

How do I create a while loop in java that will end the game once the two user inputs match?

我一直在尝试创建一个游戏,要求用户输入 2 个三字母单词;该程序应该通过将单词分开并说明字母在字母表中是在彼此之前还是之后来提供有关单词匹配程度的线索。

游戏快结束了,但是当我在猜完之后尝试开始新的回合时,我的问题出现了。我需要某种 while 循环,但我已经多次重新排列代码块,以至于我觉得这让整个事情变得更加复杂。每当两个输入不完全匹配时,应重复提示第二个用户回答问题以及提示。

Example Output when the two user inputs are cat and fan: after, a, before

The "after" shows that the letter comes after the user's letter in the alphabet, and the "before" shows that the letter comes before the user's letter.

编辑我已经尽可能多地考虑了答案,非常感谢。 到目前为止,我已经实现了一个do while循环并试图修复我的变量。 最后,我觉得我可能必须创建一个对象才能重新复制代码特定条件语句后的用户输入将被设置为 false。

我的新问题是

1.每当我在 do while 循环中有用户输入时,编译器找不到符号 x1、y1、z1、x2、y2 和 z2。

2。如果我尝试制作一个新对象,我将重写和重新排列超出可能需要的内容。 这仍在进行中,随着我继续努力,我会不断更新此 post。

(编辑代码 -- 我保存了原始代码,我可以将其发送给任何想看的人。)

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();}*/
  }
  }

也许声明 3 个布尔值(1IsCorrect、2IsCorrect、3IsCorrect)将它们默认设置为 false,并在打印值后的 if(comp/comp1/comp2 == 0) 语句中将相应的布尔值设置为 true。 然后把它放在 do while(!1IsCorrect || !2IsCorrect || !3IsCorrect) 在循环之前为 isFirstRun 声明一个变量,并在声明时将其实例化为 true,然后在最后设置为 false。 制作一个 if !isFirstRun 语句并在其中添加代码以询问下一个猜测

希望这有效