Java 如何在找到一个单词后打破循环

Java 如何在找到一个单词后打破循环,java,Java,我试图制造一个刽子手,我有两个问题 1第一个问题是当用户找到单词时,循环不会停止 2我有一个可变的尝试次数,可以知道尝试次数。即使用户找到了字母,尝试的次数也会减少 要找到的词是“不” 下面是一个演示: 1我输入字母n 2我输入字母o 3通常情况下,回路应停止 The letter is good. You have 3 attempts. no Enter your letter : 如果您有想法,请提前向您表示感谢 Scanner input = new Scanner(System.

我试图制造一个刽子手,我有两个问题

1第一个问题是当用户找到单词时,循环不会停止

2我有一个可变的尝试次数,可以知道尝试次数。即使用户找到了字母,尝试的次数也会减少

要找到的词是“不”

下面是一个演示:

1我输入字母n

2我输入字母o

3通常情况下,回路应停止

The letter is good. 
You have 3 attempts.
no
Enter your letter : 
如果您有想法,请提前向您表示感谢

Scanner input = new Scanner(System.in);

char letter = 0; 
String[] words = {/*"yes",*/ "no"}; 
String word_random = words[(int) (Math.random() * words.length)]; 
boolean[] word_found = new boolean[word_random.length()];
int attempts = 5;


while(attempts > 0){
  System.out.println("You have " + attempts + " attempts.");
  for(int i=0; i<word_random.length(); i++) {
    if ( word_found[i] ) {
      System.out.print(word_random.charAt(i));
    }
    else {
      System.out.print('-');
    }
  }

  System.out.println("");
  System.out.print("Enter your letter : ");
  letter = input.next().charAt(0);

  for(int i=0; i<word_random.length();i++){
    if(word_random.charAt(i) == letter){
      System.out.println("The letter is good. ");
      word_found[i] = true;
    } 

  }

attempts--;
   }
  }
}

我将遵循你的解决方案,而不是提出更好的解决方案。 公元1年。如果找到的数组字在第一个for循环后仅包含true,并且如果数组中仅包含true值,则添加一个检查,打印您赢得的结果并将尝试次数设置为0

公元2年。将尝试次数移动到第一个for循环的else情况,或在第一个for循环的真实情况下添加尝试次数+

public static void main(String[] args) {
    Scanner input = new Scanner(System.in);

    char letter = 0;
    String[] words = { /* "yes", */ "no" };
    String word_random = words[(int) (Math.random() * words.length)];
    boolean[] word_found = new boolean[word_random.length()];
    int attempts = 5;

    while (attempts > 0) {
        System.out.println("You have " + attempts + " attempts.");
        for (int i = 0; i < word_random.length(); i++) {
            if (word_found[i]) {
                System.out.print(word_random.charAt(i));
            } else {
                System.out.print('-');
            }
        }

        System.out.println("");
        System.out.print("Enter your letter : ");
        letter = input.next().charAt(0);

        boolean match = false;
        for (int i = 0; i < word_random.length(); i++) {
            if (word_random.charAt(i) == letter) {
                System.out.println("The letter is good. ");
                word_found[i] = true;
                match = true;
                if (i == word_found.length - 1) {
                    System.out.println("THE END: attempts: " + attempts);
                    return;
                }
            }
        }

        if (!match) {
            attempts--;
        }

    }
    System.out.println("THE END");
}

我建议您像我一样修改代码的最后一部分,它应该可以工作。

您只是缺少一个检查循环或方法。检查下面的解决方案

Scanner input = new Scanner(System.in);

char letter = 0; 
String[] words = {/*"yes",*/ "no"}; 
String word_random = words[(int) (Math.random() * words.length)]; 
boolean[] word_found = new boolean[word_random.length()];
int attempts = 5;

while(attempts > 0){
  System.out.println("You have " + attempts + " attempts.");
  for(int i=0; i<word_random.length(); i++) {
    if ( word_found[i] ) {
      System.out.print(word_random.charAt(i));
    }
    else {
      System.out.print('-');
    }
  }

  System.out.println("");
  System.out.print("Enter your letter : ");
  letter = input.next().charAt(0);

  for(int i=0; i<word_random.length();i++){
    if(word_random.charAt(i) == letter){
      System.out.println("The letter is good. ");
      word_found[i] = true;
    } 

  }

  boolean done = true;
  for(boolean b : word_found)
       done = done && b;

   if(done) break;
   else attempts--;

   }

您需要更改while循环中某个位置的尝试值。如前所述,该值始终为5。这根本不起作用。例如,如果您首先输入字母o,并且单词为“否”,则即使字母正确,程序也将退出!是的,很好,非常感谢:-但是,我不理解forech,但是只有forYou的循环可以将ForEach循环重写为:forint I=0;ipublic static void main(String[] args) { Scanner input = new Scanner(System.in); char letter = 0; String[] words = { /* "yes", */ "no" }; String word_random = words[(int) (Math.random() * words.length)]; boolean[] word_found = new boolean[word_random.length()]; int attempts = 5; while (attempts > 0) { System.out.println("You have " + attempts + " attempts."); for (int i = 0; i < word_random.length(); i++) { if (word_found[i]) { System.out.print(word_random.charAt(i)); } else { System.out.print('-'); } } System.out.println(""); System.out.print("Enter your letter : "); letter = input.next().charAt(0); boolean match = false; for (int i = 0; i < word_random.length(); i++) { if (word_random.charAt(i) == letter) { System.out.println("The letter is good. "); word_found[i] = true; match = true; if (i == word_found.length - 1) { System.out.println("THE END: attempts: " + attempts); return; } } } if (!match) { attempts--; } } System.out.println("THE END"); }
Scanner input = new Scanner(System.in);

char letter = 0; 
String[] words = {/*"yes",*/ "no"}; 
String word_random = words[(int) (Math.random() * words.length)]; 
boolean[] word_found = new boolean[word_random.length()];
int attempts = 5;

while(attempts > 0){
  System.out.println("You have " + attempts + " attempts.");
  for(int i=0; i<word_random.length(); i++) {
    if ( word_found[i] ) {
      System.out.print(word_random.charAt(i));
    }
    else {
      System.out.print('-');
    }
  }

  System.out.println("");
  System.out.print("Enter your letter : ");
  letter = input.next().charAt(0);

  for(int i=0; i<word_random.length();i++){
    if(word_random.charAt(i) == letter){
      System.out.println("The letter is good. ");
      word_found[i] = true;
    } 

  }

  boolean done = true;
  for(boolean b : word_found)
       done = done && b;

   if(done) break;
   else attempts--;

   }