如何在循环内设置if语句

时间:2017-06-22 23:35:27

标签: java arrays loops

我对此非常陌生,这只是我班上的第二个项目。 当我运行它时,无论我的输入是什么,x总是>我跑了调试器,发现它在城市1和2都增加x,所以我想我没有正确设置我的if语句。我们被指示提示用户访问3个城市,如果有匹配,我们会告诉他们。提前谢谢!

import java.util.Scanner; 公共类PR2 {

public static void main(String[] args) 
{
    int x = 0;
    Scanner input = new Scanner(System.in);
    String[] City = new String[3];
    City[0] = "Orlando";
    City[1] = "New York";


    for (int i = 0; i < 3; i++)
    {
        System.out.println("Please enter a city you have visited.");
        City[i] = input.nextLine();
    }

    for (int i = 0; i < 3; i++)
    {   
        if (City[i] == City[0])
        {
            x++;
        }
        else if (City[i] == City[1])
        {
            x++;
        }
    }
    if (x > 0)
    {
        System.out.println("At least one city you have entered is a match.");
    }
    if (x == 0)
    {
        System.out.println("Sorry, none of the cities you have entered are a match.");
    }


}

}

0 个答案:

没有答案