-3

我正在为用户编写一个程序来添加一个字符串ArrayList然后显示它。它不起作用,似乎有问题compareTo()

这是我的代码:

public class database {

    static BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    static String country[] = new String[100];
    static String capital[] = new String[100];
    static double population[] = new double[100];
    static List<String> countriesList = Arrays.asList(country);

    public static void main(String args[]) throws IOException {
        country[0] = "Barbados";
        country[1] = "France";
        country[2] = "Nigeria";
        country[3] = "USA";
        country[4] = "Japan";
        capital[0] = "Bridgetown";
        capital[1] = "Paris";
        capital[2] = "Abuja";
        capital[3] = "Washington";
        capital[4] = "Tokyo";
        population[0] = 65.3;
        population[1] = 315.8;
        population[2] = 170.1;
        population[3] = 2840;
        population[4] = 126.7;

    public static void searchCountry() throws IOException {
        Scanner in = new Scanner(System.in);
        String output;
        int size, i;
        System.out.println("Search Country:");
        output = br.readLine();
        boolean found = false;
        for (i = 0; i < country.length; i++)
            if (output.compareTo(country[i]) == 0) {
                found = true;
                break;
            }
        if (found)
            System.out.println(output + " is found at index " + i);
        else
            System.out.println(output + "Country not found, choose Add country to add it");

    public static void listCountry() throws IOException {
        for (String c : countriesList) {
            if (!=null)
            System.out.println(c);

        }
    }
}

null我的代码末尾的也有问题。

4

3 回答 3

1

编写代码时,最好从头开始。IE

首先,写下类名并确保括号没有问题

public class MyClass{
}

然后,在其中编写 main 方法。

public class MyClass{
    public static void main(String[] args){
    }
}

然后,编写您的方法,并在 main 方法中对其进行测试。

public class MyClass{
    public void mymethod(){
        //do something
        System.out.println("say something");
    }
    public static void main(String[] args){
         MyClass mc = new MyClass();
         mc.mymethod();
    }
}

如果您尝试在一个镜头中编写所有内容,它将无法正常工作,如果您不是专家,那么您将很难解决问题。

于 2013-05-29T16:24:40.330 回答
0
  1. 您不能在方法中编写方法。main()在打开括号之前关闭括号searchCountry()
  2. 您不会检查任何内容是否为空。也许你的意思是if(c != null)

就这样写,你应该没问题:

public class database {

    static BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    static String country[] = new String[100];
    static String capital[] = new String[100];
    static double population[] = new double[100];
    static List<String> countriesList = Arrays.asList(country);

    public static void main(String args[]) {
        country[0] = "Barbados";
        country[1] = "France";
        country[2] = "Nigeria";
        country[3] = "USA";
        country[4] = "Japan";
        capital[0] = "Bridgetown";
        capital[1] = "Paris";
        capital[2] = "Abuja";
        capital[3] = "Washington";
        capital[4] = "Tokyo";
        population[0] = 65.3;
        population[1] = 315.8;
        population[2] = 170.1;
        population[3] = 2840;
        population[4] = 126.7;
        searchCountry();
        listCountry();
    }

    public void searchCountry() throws IOException {
        Scanner in = new Scanner(System.in);
        String output;
        int size, i;
        System.out.println("Search Country:");
        output = br.readLine();
        boolean found = false;
        for (i = 0; i < country.length; i++)
            if (output.compareTo(country[i]) == 0) {
                found = true;
                break;
            }
        if (found)
            System.out.println(output + " is found at index " + i);
        else
            System.out.println(output + "Country not found, choose Add country to add it");
    }

    public void listCountry() {
        for (String c : countriesList) {
            if (c!=null)
                System.out.println(c);
        }
    }
}
于 2013-05-29T16:22:31.903 回答
0

你忘了c输入c != null
您不能在 java 中的方法内部定义方法。
此外,equals在测试String相等性时使用该方法,即if (output.equals(country [i])).

于 2013-05-29T16:22:36.560 回答