如何将字符串数组与用户输入进行比较以查看是否正确

时间:2015-12-27 06:33:09

标签: java

这与我用来比较来自用户输入的数字与" int"基本相同,但由于某种原因,即使它是正确的,它也不适用于字符串数组中的单词。它未完成,我只是想让它正确比较。

            case 2:

            cell = (int)((Math.random() * 59) + 1);
            System.out.println(englishNumbers[cell]);
            System.out.println("what is the number? >");
            String w = in.nextLine();
            if(w != frenchNumbers[cell])
            {
                System.out.println("That is incorrect");
                numChoice(2);
            }
            else
            {
                System.out.println("That's correct");
                numChoice(2);
            }
            break;

1 个答案:

答案 0 :(得分:0)

使用if(!w.equals(frenchNumbers[cell]))

而不是

if(w != frenchNumbers[cell])

您必须比较值,而不是参考。

相关问题