简体   繁体   中英

What's wrong with this randomized string and if-statement in java

I'm wetting my feet in java, and I've stumbled upon an issue. I suspect there might be already an answer for it, but I'm too much of a novice to know what it is I should be looking for - it also means my terminology might not be correct.

In the following code, I'm trying to create a random choice from an array of words, then use an if-statement to either display or not display the word. The problem is, although the condition of the if-statement is met (either the string "cat" or the string "dog" are obtained), the action displays any of the listed words, instead of either "cat" or "dog"

I suspect that when I use System.out.println(exampleWord()); in line 10, the routine obtains a new value from the array, effectively ignoring the if-statement.

What's the easiest way around this?

import java.util.Random; 

public class Phrasing {
    String word;

    public static void main(String[] args) {
        int i = 1;
        while (i < 9) {
            if ("cat".equals(exampleWord()) || "dog".equals(exampleWord())) {
                System.out.println(exampleWord()); 
                i++;
            }
        }
    }

    public static String exampleWord() {
        String[] listedWords = {"cat", "dog", "horse", "fish", "turtle", "mouse"};
        Random random = new Random();
        int index = random.nextInt(listedWords.length);
        Phrasing wordOutput;
        wordOutput = new Phrasing();
        wordOutput.word = listedWords[index];
        return (wordOutput.word);
    }
}

You should generate the word only once and then perform checks and output it:

while (i < 9) {
    String exampleWord = exampleWord();
    if ("cat".equals(exampleWord) || "dog".equals(exampleWord)){
        System.out.println(exampleWord); 
        i++;
    }
}

Yes, you are right. Each time you call exampleWord() , a random word is generated. Try storing it in a String once.

public static void main(String[] args) {
    int i = 1;
    while (i < 9) {
        String s = exampleWord();
        if ("cat".equals(s) || "dog".equals(s)) {
            System.out.println(s); 
            i++;
        }
    }
}

Also, it seems like you are doing some work unnecessarily, in the exampleWord method. You could do

public static String exampleWord() {
    String[] listedWords = {"cat", "dog", "horse", "fish", "turtle", "mouse"};
    Random random = new Random();
    return listedWords[random.nextInt(listedWords.length)];
}

The problem is that you are executing exampleWord() every time you compare "cat" or "dog" to the result, and then again you execute it to print the result. Just execute exampleWord() one time inside the loop, store it in a variable and finally compare and print the result. Something like:

String result = exampleWord()
if("cat".equals(result) || "dog".equals(result)) {
    System.out.println(result); 
    i++;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM