阵列显示方法难度

时间:2013-11-04 11:13:46

标签: java arrays

新手在这里。所以我正在测试数组,这是我一直在阅读的教程中的下一个主题。我做了一个简单的程序来指定要输入或输入的数字。问题出在getArray()方法上,我似乎无法显示它。它在线程错误中给了我一个例外。非常感谢任何帮助和建议。 :)抱歉新手问题。 :)) 所以这是代码。

import java.util.Scanner;

public class ArrayNumbers {

Scanner input = new Scanner(System.in);
int totalNum, counter, count = 0, display;
double arrayNum[];

public void setArrayNum() {
    System.out.print("How many numbers?: ");
    totalNum = input.nextInt();
    double arrayNum[] = new double[totalNum];

    for (counter = 0 ; counter < arrayNum.length ; counter++) {
        System.out.print("Num "+(count = counter + 1)+": ");
        arrayNum[counter] = input.nextInt(); 
    }
}

public void getArray() {
    System.out.print("What number to display?: ");
    display = input.nextInt();

    System.out.print(arrayNum[display]);
}
}

import java.util.Scanner;

public class MainProgram {

public static void main(String[] args) {
    Scanner input = new Scanner(System.in);
    Numbers NumbersObject = new Numbers();
    ArrayNumbers ArrayNumbersObject = new ArrayNumbers();
    String name;
    int choice;

    System.out.print("Enter name: ");
    name = input.nextLine();
    System.out.println("Menu");
    System.out.println("1. Math Operations");
    System.out.println("2. Grade Computation");
    System.out.println("3. Counting Numbers");
    System.out.println("4. Array Numbers");
    System.out.print("Enter choice: ");
    choice = input.nextInt();

    switch(choice) {

    case 1:
        System.out.println("Choose Operation");
        System.out.println("1. Addition");
        System.out.println("2. Subtraction");
        System.out.println("3. Multiplication");
        System.out.println("4. Division");
        System.out.print("Enter choice: ");
        choice  = input.nextInt();

        NumbersObject.setNum();

        if (choice == 1)
            System.out.println("The answers is "+(NumbersObject.getNum1() + NumbersObject.getNum2()));
        else if (choice == 2)
            System.out.println("The answers is "+(NumbersObject.getNum1() - NumbersObject.getNum2()));
        else if (choice == 3)
            System.out.println("The answers is "+(NumbersObject.getNum1() * NumbersObject.getNum2()));
        else if (choice == 4)
            System.out.println("The answers is "+(NumbersObject.getNum1() / NumbersObject.getNum2()));
        else
            System.out.print("Invalid Input!");
        break;

    case 2:
        NumbersObject.setNum();
        System.out.println("Your average grade is "+((NumbersObject.getNum2() + NumbersObject.getNum2()) / 2));
        break;

    case 3:
        System.out.println("Welcome to Counting Numbers!");
        System.out.println("Enter 2 numbers to start and end!");
        NumbersObject.setNum();

        for (int counter = (int) NumbersObject.getNum1() ; counter <= NumbersObject.getNum2() ; counter++) {
            System.out.println(counter);
        }

        System.out.println("End!");
        break;

    case 4:
        ArrayNumbersObject.setArrayNum();
        ArrayNumbersObject.getArray();
        break;

    default:
        System.out.println("Mr/Ms "+name+" you entered an Invalid Choice!");
        break;
    }//end of switch    
}// end of main
}// end of class

很抱歉,因为不包括主页。

这是错误:

Exception in thread "main" java.lang.NullPointerException
at ArrayNumbers.getArray(ArrayNumbers.java:23)
at MainProgram.main(MainProgram.java:67)

伙计们,我唯一遇到的问题是在getArray()中 它询问我希望看到的数组中的数字,但是当我输入它时,它在线程错误中给出了一个异常。唯一的问题是我将如何显示数组[我指定的数字]?对不起新手问题。

3 个答案:

答案 0 :(得分:2)

它会抛出异常,因为您创建了一个大小显示的数组,并且您尝试在显示位置打印该元素,但arrays在Java中为0基本索引。

这个数字应该是有用的:

enter image description here

想象一下display = 10

double arrayNum[] = new double[display]; //create an array that can holds 10 elements
System.out.print(arrayNum[display]); //try to access at the 10th element of the array but it doesn't exists !

您不必创建新阵列,因为您希望在班级中创建的arrayNum中显示该号码。

public void getArray() {
    System.out.print("What number to display?: ");
    display = input.nextInt();
    System.out.print(arrayNum[display]);
}

在尝试访问其元素之前,您还需要检查display是否在正确的范围[0,...,displayNum.length-1]内。

修改:

setArrayNum()double arrayNum[] = new double[totalNum];替换arrayNum = new double[totalNum];

答案 1 :(得分:0)

数组是从0开始的。因此,如果您创建大小为display的数组,则可以访问索引0..display-1

答案 2 :(得分:0)

您正在double arrayNum[] = new double[display];方法中创建本地数组getArray()

您需要从double arrayNum[] = new double[display];方法移除getArray()

之后您的方法如下。

public void getArray() {
    System.out.print("What number to display?: ");
    display = input.nextInt();
    if(display >0 && display <=arrayNum.length){
            System.out.print(arrayNum[display-1]);
    }else{
         System.out.print(display + " is out of range");
    }
}