搜索数组时无法找到符号以进行精确匹配

时间:2018-03-22 01:04:24

标签: java

我是编码的初学者,如果这是一个愚蠢的问题/如果我错过了任何标签,那么道歉。我编写了一个使用10个城市阵列的程序。输出是要求用户输入一个城市,程序将告诉该数组是否包含它。在第22和23行,我收到错误:"找不到符号"。这些内容如下:

for(int i = 0; i < citiesInMichigan.length; i++){
            if (citiesInMichigan[i].equals(inCity))

如果有人可以阅读我的代码并告诉我我做错了什么,我们将不胜感激。提前谢谢!

// MichiganCities.cpp - This program prints a message for invalid cities in Michigan. 
// Input: Interactive
// Output: Error message or nothing

import javax.swing.*;

public class MichiganCities
{
    public static void main(String args[]) throws Exception
    {
        // Declare variables.
        String inCity;  // name of city to look up in array.
        // Initialized array of cities in Illinois.
        String citiesInIllinois[] = {"Acme", "Albion", "Detroit", "Watervliet", "Coloma", "Saginaw", "Richland", "Glenn", "Midland", "Brooklyn"}; 
        boolean foundIt = false;  // Flag variable.
        int x;  // Loop control variable.

        // Get user input.
        inCity = JOptionPane.showInputDialog("Enter name of city: ");

        // Write your loop here. 
        for(int i = 0; i < citiesInMichigan.length; i++){
            if (citiesInMichigan[i].equals(inCity))
                foundIt = true;
        }
            // Write your test statement here to see if there is 
            // a match.  Set the flag to true if city is found. 


        if (foundIt)
            System.out.println("is in Michigan");
        else System.out.println("Not a city in Michigan");


        // Test to see if city was not found to determine if 
        // "Not a city in Illinois" message should be printed. 

        System.exit(0);
    }
}

编辑:我一直收到此错误:

Line 19: Exception in thread "main" java.awt.AWTError: Can't connect to X11 window server using ':1' as the value of the DISPLAY variable.
at sun.awt.X11GraphicsEnvironment.initDisplay(Native Method)
at sun.awt.X11GraphicsEnvironment.access$200(X11GraphicsEnvironment.java:65)
at sun.awt.X11GraphicsEnvironment$1.run(X11GraphicsEnvironment.java:115)
at java.security.AccessController.doPrivileged(Native Method)
at sun.awt.X11GraphicsEnvironment.<clinit>(X11GraphicsEnvironment.java:74)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Class.java:264)
at java.awt.GraphicsEnvironment.createGE(GraphicsEnvironment.java:103)
at 
at sun.awt.X11.XToolkit.<clinit>(XToolkit.java:126)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Class.java:264)
at java.awt.Toolkit$2.run(Toolkit.java:860)
at java.awt.Toolkit$2.run(Toolkit.java:855)
at java.security.AccessController.doPrivileged(Native Method)
at java.awt.Toolkit.getDefaultToolkit(Toolkit.java:854)
at javax.swing.UIManager.setLookAndFeel(UIManager.java:539)
at javax.swing.UIManager.setLookAndFeel(UIManager.java:579)
at javax.swing.UIManager.initializeDefaultLAF(UIManager.java:1349)
at javax.swing.UIManager.initialize(UIManager.java:1459)
at javax.swing.UIManager.maybeInitialize(UIManager.java:1426)
at javax.swing.UIManager.getDefaults(UIManager.java:659)
at javax.swing.UIManager.getString(UIManager.java:805)
at javax.swing.UIManager.getString(UIManager.java:822)
at javax.swing.JOptionPane.showInputDialog(JOptionPane.java:474)
at javax.swing.JOptionPane.showInputDialog(JOptionPane.java:440)
at MichiganCities.main(MichiganCities.java:19).

第19行读到:

inCity = JOptionPane.showInputDialog("Enter name of city: ");

1 个答案:

答案 0 :(得分:-1)

您宣布&#34; citiesInIllinois&#34;,并参考&#34; citiesInMichigan&#34;。