gpt4 book ai didi

java - 数组显示法难点

转载 作者:行者123 更新时间:2023-11-30 04:10:19 25 4
gpt4 key购买 nike

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

最佳答案

它会抛出异常,因为您创建了一个显示大小的数组,并且尝试在显示位置打印该元素,但 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];

关于java - 数组显示法难点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19766553/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com