如何找到一个单词后从循环中中断

问题描述 投票:-1回答:3

我正在尝试创建一个Hang子手,有两个问题。

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

2)我有一个变量attempts,它允许知道尝试次数。即使用户找到字母,尝试的次数也会减少。

要查找的单词是no

这里是演示:

1)我输入字母n

You have 5 attempts.
--
Enter your letter : n

2)我输入字母o

The letter is good. 
You have 4 attempts.
n-
Enter your letter : 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--;
   }
  }
}
java
3个回答
0
投票

我将遵循您的解决方案,而不是提出更好的解决方案。广告1.添加一个检查,如果找到的数组单词在第一个for循环之后仅包含true,并且数组中只有true值,则打印“ you won”并将尝试设置为0

广告2。将尝试移动到第一个循环的其他情况,或在第一个循环的真实情况下添加尝试++


0
投票
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");
}

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


0
投票

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

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

   }
© www.soinside.com 2019 - 2024. All rights reserved.