1

新手在这里。所以我正在测试数组,这是我一直在阅读的教程中的下一个主题。我做了一个简单的程序来指定要获取或输入多少个数字。问题出在 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() 它询问我想查看的数组中的什么数字,但是当我输入它时,它在线程错误中给出了一个异常。唯一的问题是我将如何显示数组[我指定的数字]?抱歉新手问题。

4

3 回答 3

2

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

这个数字应该很有用:

在此处输入图像描述

所以想象一下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];

于 2013-11-04T11:15:43.340 回答
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");
    }
}
于 2013-11-04T11:16:41.830 回答
0

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

于 2013-11-04T11:16:10.843 回答