为什么我的循环在输入正确的字符串数组值后不重复?

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

Why does my Loop not repeat after inputting a correct value from an String Array?

问题

我正在创建一个基本的单词拼图程序作为个人项目,用户会看到一组字母,然后从这组字母中创建尽可能多的单词(例如:I T E R; rite, tire, er)。每当他们从String数组中的匹配项中得到一个正确的单词时,会累积一个分数。如果猜测错误(猜测不在数组中),则会显示最终分数和所花费的时间。我的问题是,当我输入一个值(re),然后输入另一个正确的值(tire)时,循环不允许我添加另一个猜测,而是退出循环,分数只更新到第一个正确的猜测,而不是两者都更新。如何改变逻辑,以便我可以以任何顺序输入数组列表中的任何单词并获得正确的分数?

我尝试将cont布尔值移出for循环。我尝试在检查数组项不等于输入时,使用else if语句添加cont布尔值。但问题仍然存在,我可以输入"re"然后"tire",程序就停止了。以下是我的代码。

import java.sql.SQLOutput;
import java.time.Duration;
import java.time.Instant;
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Instant starts = Instant.now();
        String guess;

        int score = 0;
        Scanner input = new Scanner(System.in);
        String seconds;
        String[] puzzle1 = {"iter", "rite", "tier", "tire", "trie",
                "ire", "rei", "ret", "rit", "tie", "er", "et", "re", "te", "ti"};
        Boolean cont = true;

        System.out.println("How many words can you create with the following letters:" +
                "\n  T   I   E   R");
        System.println("Enter a guess: ");
        String userInput = input.nextLine();
        int k= 0;
        while (cont) {

            for (int i = 0; i < puzzle1.length; i++) {

                if (puzzle1[i].equals(userInput)) {
                    score += 100;
                    System.out.println("Good! Enter another guess: ");
                    userInput = input.nextLine();
                }

            }
            cont = false;

        }

        Instant ends = Instant.now();
        long mins = Duration.between(starts, ends).toMinutes();
        long time = Duration.between(starts, ends).toSeconds();
        long actual = time % 60;

        if (time <= 9) {
            seconds = "0" + actual;
        } else {
            seconds = String.valueOf(time);
        }

        System.out.println("Your time was " + mins + ":" + seconds + " and with a score of " + score + " points.");
    }
}

以下是输出:

How many words can you create with the following letters:
  T   I   E   R
Enter a guess: 
re
Good! Enter another guess: 
tire
Your time was 0:05 and with a score of 100 points.
英文:

I'm creating a basic word puzzle program as a personal project where a user sees a set of letters and has to create as many words from that set of letters (ie. I T E R; rite, tire, er). A score is added up for each time they get a correct word from one of the matches in a String array. If an incorrect guess is made (the guess does not appear in the array), a final score and the time it took are displayed. My problem is that after I input a value (re) and then enter another correct value (tire), the loop doesn't allow me to add another guess but exits the loop, and the score only gets updated to the first correct guess, not both. How can i change the logic so that i can enter any of the words from the array list in any order and get the correct score?

I tried to move the cont boolean outside the for loop. I tried to add the cont boolean in an else if statement when checking the if array item does not equal the input. Same issue persisted, i can enter re then tire and the program stops. Here is my code.

import java.sql.SQLOutput;
import java.time.Duration;
import java.time.Instant;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Instant starts = Instant.now();
String guess;
int score = 0;
Scanner input = new Scanner(System.in);
String seconds;
String[] puzzle1 = {&quot;iter&quot;, &quot;rite&quot;, &quot;tier&quot;, &quot;tire&quot;, &quot;trie&quot;,
&quot;ire&quot;, &quot;rei&quot;, &quot;ret&quot;, &quot;rit&quot;, &quot;tie&quot;, &quot;er&quot;, &quot;et&quot;, &quot;re&quot;, &quot;te&quot;, &quot;ti&quot;};
Boolean cont = true;
System.out.println(&quot;How many words can you create with the following letters:&quot; +
&quot;\n  T   I   E   R&quot;);
System.out.println(&quot;Enter a guess: &quot;);
String userInput = input.nextLine();
int k= 0;
while (cont) {
for (int i = 0; i &lt; puzzle1.length; i++) {
if (puzzle1[i].equals(userInput)) {
score += 100;
System.out.println(&quot;Good! Enter another guess: &quot;);
userInput = input.nextLine();
}
}
cont = false;
}
Instant ends = Instant.now();
long mins = Duration.between(starts, ends).toMinutes();
long time = Duration.between(starts, ends).toSeconds();
long actual = time % 60;
if (time &lt;= 9) {
seconds = &quot;0&quot; + actual;
} else {
seconds = String.valueOf(time);
}
System.out.println(&quot;Your time was &quot; + mins + &quot;:&quot; + seconds + &quot; and with a score of &quot; + score + &quot; points.&quot;);
}}

here is the output

How many words can you create with the following letters:
T   I   E   R
Enter a guess: 
re
Good! Enter another guess: 
tire
Your time was 0:05 and with a score of 100 points.

答案1

得分: 0

只需要解决关于 cont = false 的逻辑问题,即,您想让这个程序在什么情况下终止,也许是答案错误?

保持 cont = true 直到您准备离开循环。此外,您可能希望考虑使用 List<String> 而不是 String[],这允许您使用 contains 方法来替代嵌套的 for 循环。

例如,用以下代码替代:

if(puzzle.contains(userInput)) {
   score += 100;
   System.out.println("Good! Enter another guess: ");
   userInput = input.nextLine();
} else {
   System.println("Terrible!: ");
   cont = false;
}
英文:

It seems like you just need to figure out your logic around cont = false, as in, what would you want to make this program terminate, a wrong answer maybe?

Keep cont = true until you're ready to leave the loop. Also, you may want to consider using a List&lt;String&gt; instead of String[], this allows you to remove the nested for-loop in favor of the contains method.

e.g. replace

for (int i = 0; i &lt; puzzle1.length; i++) {
if (puzzle1[i].equals(userInput)) {
score += 100;
System.out.println(&quot;Good! Enter another guess: &quot;);
userInput = input.nextLine();
}
}

with simply

if(puzzle.contains(userInput)) {
score += 100;
System.out.println(&quot;Good! Enter another guess: &quot;);
userInput = input.nextLine();
} else {
System.out.println(&quot;Terrible!: &quot;);
cont = false;
}

huangapple
  • 本文由 发表于 2023年2月7日 02:28:56
  • 转载请务必保留本文链接:https://go.coder-hub.com/75365242.html
匿名

发表评论

匿名网友

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

确定