Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/335.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
什么';java中的随机字符串和if语句有什么问题_Java_If Statement - Fatal编程技术网

什么';java中的随机字符串和if语句有什么问题

什么';java中的随机字符串和if语句有什么问题,java,if-statement,Java,If Statement,我在java中弄湿了脚,我偶然发现了一个问题。我怀疑可能已经有了答案,但我太新手了,不知道我应该寻找什么——这也意味着我的术语可能不正确 在下面的代码中,我试图从单词数组中创建一个随机选择,然后使用if语句显示或不显示单词。问题是,尽管满足了if语句的条件(获得了字符串“cat”或字符串“dog”),但操作将显示列出的任何单词,而不是“cat”或“dog” 我怀疑当我使用System.out.println(exampleWord())时在第10行,例程从数组中获取一个新值,实际上忽略了if语句

我在java中弄湿了脚,我偶然发现了一个问题。我怀疑可能已经有了答案,但我太新手了,不知道我应该寻找什么——这也意味着我的术语可能不正确

在下面的代码中,我试图从单词数组中创建一个随机选择,然后使用if语句显示或不显示单词。问题是,尽管满足了if语句的条件(获得了字符串“cat”或字符串“dog”),但操作将显示列出的任何单词,而不是“cat”或“dog”

我怀疑当我使用
System.out.println(exampleWord())时
在第10行,例程从数组中获取一个新值,实际上忽略了if语句

最简单的方法是什么

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);
    }
}
import java.util.Random;
公共类措辞{
字符串字;
公共静态void main(字符串[]args){
int i=1;
而(i<9){
如果(“cat.equals”(exampleWord())| |“dog.equals”(exampleWord())){
System.out.println(exampleWord());
i++;
}
}
}
公共静态字符串exampleWord(){
String[]listedWords={“猫”、“狗”、“马”、“鱼”、“龟”、“鼠”};
随机=新随机();
int index=random.nextInt(listedWords.length);
措辞输出;
wordOutput=新短语();
wordOutput.word=listedWords[index];
返回(wordOutput.word);
}
}

您应该只生成一次单词,然后执行检查并输出它:

while (i < 9) {
    String exampleWord = exampleWord();
    if ("cat".equals(exampleWord) || "dog".equals(exampleWord)){
        System.out.println(exampleWord); 
        i++;
    }
}
while(i<9){
字符串exampleWord=exampleWord();
如果(“猫”.equals(例如单词)| |“狗”.equals(例如单词)){
System.out.println(exampleWord);
i++;
}
}

是的,你说得对。每次调用
exampleWord()
,都会生成一个随机单词。尝试将其存储在
字符串中一次

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

问题是,每次将“cat”或“dog”与结果进行比较时,您都在执行exampleWord(),然后再次执行它以打印结果。 只需在循环中执行exampleWord()一次,将其存储在变量中,最后比较并打印结果。 比如:

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

很好,你提出了修改建议。我本来也打算提出同样的建议,请接受我的投票!:)
String result = exampleWord()
if("cat".equals(result) || "dog".equals(result)) {
    System.out.println(result); 
    i++;
}