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

huangapple go评论81阅读模式
英文:

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

问题

import java.util.Scanner;

class Main{
  public static void main(String[] args) {
    System.out.println("猜词游戏\n(需要两名玩家)");
    System.out.println("如果每个单词中的字母匹配,该字母将被重新打印。");
    System.out.println("如果猜测的字母不匹配,将打印\"before\"(在之前)或\"after\"(在之后)。");
    Scanner in1 = new Scanner(System.in);

    try{
      boolean correct1 = false;
      boolean correct2 = false;
      boolean correct3 = false;
      int indication = 0;

      do{
        System.out.print("第一名玩家,请输入一个三个字母的单词:");
        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("第二名玩家,请输入一个三个字母的单词:");
        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);

      int comp;
      int comp2;
      int comp3;
      comp = x1.compareTo(x2);
      comp2 = y1.compareTo(y2);
      comp3 = z1.compareTo(z2);

      if(comp == 0){
        System.out.print(x1);
        correct1 = true;
      }
      else{
        if(comp > 0){
          System.out.println("before");
        }
        else{
          System.out.print("after");
        }
      }

      if(comp2 == 0){
        System.out.print(y1);
        correct2 = true;
      }
      else{
        if(comp2 > 0){
          System.out.println(", before");
        }
        else{
          System.out.print(", after");
        }
      }

      if(comp3 == 0){
        System.out.print(z1);
        correct3 = true;
      }
      else{
        if(comp3 > 0){
          System.out.println(", before");
        }
        else{
          System.out.print(", after");
        }
      }
    }
    finally{
      in1.close();
    }
  }
}
英文:

I have been trying to create a game that asks the user to type in 2 three-letter words; the program is supposed to give clues for how close the words match by splitting them up and stating if the letters come before, or after, each other in the alphabet.

The game is nearly done, but my problem shows up when I try to start a new turn after one guess. I need some kind of while loop, but I've rearranged blocks of the code so many times that I feel like it made the entire thing more convoluted. The prompt for the second user to answer a question, as well as the clue, should be repeated every time the two inputs don't fully match.

> 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.

EDIT: I have taken the answers into account as much as I can, thank you so much. So far, I have implemented a do while loop and tried to fix my variables. In the end, I feel that I may have to create an object to be able to recopy the code of the user inputs after a certain conditional statement would be set to false.

My new issues are

1. The compiler cannot find the symbols x1,y1,z1,x2,y2, and z2 whenever I have the user inputs inside the do while loop.

2. If I tried to make a new object, I would be rewriting & rearranging more than what might be necessary.
This is still ongoing, and as I continue to work on it I will keep updating this post.

(EDITED CODE -- I saved the original code which I can send to anyone who would like to see it.)

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

答案1

得分: 0

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

希望这样能够正常工作。

英文:

Maybe declare 3 booleans (1IsCorrect, 2IsCorrect, 3IsCorrect) set them default to false and in the if(comp/comp1/comp2 == 0) statements after printing the value set the corresponding boolean to true.
then put it in a do while(!1IsCorrect || !2IsCorrect || !3IsCorrect)
declare a variable perhaps for isFirstRun before loop and instantiate it to true at the declaration, then set to false at the very end.
Make an if !isFirstRun statement and add in the code there to ask for the next guess

Hope this works

huangapple
  • 本文由 发表于 2020年10月1日 06:18:01
  • 转载请务必保留本文链接:https://go.coder-hub.com/64146525.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定