Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/305.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 为什么我的简单数组不能工作?_Java_Arrays - Fatal编程技术网

Java 为什么我的简单数组不能工作?

Java 为什么我的简单数组不能工作?,java,arrays,Java,Arrays,这个程序应该询问有多少动物被留在野外5次。然后它应该使用第二种方法来输出相同的信息。但是我想不出来;每次我根据前面的问题修改任何内容时,我只会增加错误的数量 import java.util.Scanner; class animals { public static void main(String[] args) { int[] q1 = question(); output(q1); System.exit(0);

这个程序应该询问有多少动物被留在野外5次。然后它应该使用第二种方法来输出相同的信息。但是我想不出来;每次我根据前面的问题修改任何内容时,我只会增加错误的数量

import java.util.Scanner;

class animals {

    public static void main(String[] args) {

        int[] q1 = question();
        output(q1);

        System.exit(0);

    } // exit main

    public static int[] question() {
        String[] wild = { "Komodo Dragon", "Mantee", "Kakapo", "Florida Panther", "White Rhino" };
        int number = 0;
        int[] record = {};
        for (int i = 1; i <= 5; i++) {
            System.out.println(wild[number] + ":");
            Scanner scanner = new Scanner(System.in);
            System.out.println("How many are left in the wild?");
            int howMany = scanner.nextInt();
            record = new int[] {howMany};
            number++;

        }//end for loop

        return record;

    }// end method question

    public static void output(int[] q1){
        System.out.println("There are " + q1[0] +  " Komodo Dragons in the wild");
        System.out.println("There are " + q1[1] +  " Mantees in the wild");
        System.out.println("There are " + q1[2] +  " Kakapos in the wild");
        System.out.println("There are " + q1[3] +  " Florida Panthers in the wild");
        System.out.println("There are " + q1[4] +  " White Rhinos in the wild");
    }//end method output

} // end class animals

除了我得到文本这一事实之外,提供的monodo dragon号码是我输入的最后一个号码,而不是第一个。这没有意义

int[number] record = {};
最像你的意思是

int[] record = new int[wild.length];
而不是

for (int i = 1; i <= 5; i++) {
当您尝试访问
[1]

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 1
你需要

for (int i = 0; i < wild.length; i++) {
record[i] = howMany;

当您在IDE(或编辑器)中编写每一行代码时,您应该看到它是否编译,以及它是否不编译。添加更多的代码行不太可能有帮助。我建议您尽可能经常地进行编译和测试,这样您就可以知道错误的来源,当您遇到错误时,您可以逐步检查调试器中的代码,以了解为什么程序没有按预期执行。

这没有意义

int[number] record = {};
最像你的意思是

int[] record = new int[wild.length];
而不是

for (int i = 1; i <= 5; i++) {
当您尝试访问
[1]

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 1
你需要

for (int i = 0; i < wild.length; i++) {
record[i] = howMany;
当您在IDE(或编辑器)中编写每一行代码时,您应该看到它是否编译,以及它是否不编译。添加更多的代码行不太可能有帮助。我建议您尽可能多地编译和测试,以便知道错误的来源,当您遇到错误时,您可以逐步检查调试器中的代码,以了解程序没有按预期运行的原因。

这就是您需要的:

int number=0;
int[]记录=新的int[5]

以及您需要进行的另一项修改:

int howMany=scanner.nextInt();
记录[数量]=多少

从最后一行删除注释

现在你的程序应该可以正常工作了

了解有关阵列的一些基本知识。

这是您需要的:

int number=0;
int[]记录=新的int[5]

以及您需要进行的另一项修改:

int howMany=scanner.nextInt();
记录[数量]=多少

从最后一行删除注释

现在你的程序应该可以正常工作了


学习一些关于数组的基本知识。

请注意,
System.exit(0)
在主要方法的末尾是无用的。我将从编译的代码开始,当您添加每一行时,确保在添加更多
int[number]record={}之前编译它
永远不会编译,在这种情况下,添加更多代码只会让人越来越困惑。注意:数组从数组0开始,而不是从数组1开始。每次调用
record=newint[]{howMany}替换上一个值。只保留最后一个值。请注意,
System.exit(0)
在主方法末尾是无用的。我将从编译的代码开始,当您添加每一行时,确保在添加更多
int[number]record={}之前编译它
永远不会编译,在这种情况下,添加更多代码只会让人越来越困惑。注意:数组从数组0开始,而不是从数组1开始。每次调用
record=newint[]{howMany}替换上一个值。仅保留最后一个值。