我在初学Java类中,只是学习数组的概念。我们必须使用注释来表示代码。
我正在尝试创建一个程序,要求用户输入20个数字,将它们存储在一个数组中,然后计算并显示:最小数字,最高数字,数字总数和数字平均值。
通过jGrasp运行时出现了大量错误,我不确定如何修复它们。
有什么建议吗?
public class NumberAnalysisProgram
{
public static void main (String[] args)
{
Scanner input = new Scanner (System.in);
//A constant for the array size
final int SIZE = 20;
//Declare an array to hold the numbers entered by the user
int[] numbers = new int[SIZE];
//Declare variables to hold the lowest and highest
int lowest = numbers[0];
int highest = numbers[0];
//Declare variable to hold the total
int sum;
//Declare a variable to hold the average
double average;
//Declare a counting variable to use in the loops
int index = 0;
//Explain the program
System.out.println("This program gets a list of 20 numbers. Then displays:");
System.out.println(" the lowest number, the highest number, the total of the numbers,
and the average.");
//Get the numbers from the user
for (int i = 0; i< numbers.length; i++)
{
System.out.print("Please enter 20 numbers, each seperated by a space: ");
numbers[i] = input.nextInt();
}
//Call a method to calculate the lowest and highest numbers
getLowHigh(numbers);
//Display the lowest and highest numbers
System.out.println("The lowest number is: " + lowest);
System.out.println("The highest number is: " + highest);
//Call a method to calculate the total of the numbers
sum = getTotal(numbers);
//Display the sum/total of the numbers
System.out.println("The total of these numbers are: " + sum);
//Call a method to calculate the average of the numbers
average = getAverage(sum, numbers);
//Display the average of the numbers
System.out.println("The average of these numbers are: " + average);
}
//Method getLowHigh
public static int getLowest(int[] array)
{
for(int i=1; i< numbers.length; i++)
{
if(numbers[i] > highest)
highest = numbers[i];
else if (numbers[i] < lowest)
lowest = numbers [i];
}
return highest;
return lowest;
}
//Method getTotal
public static int getTotal(int[] array)
{
//Loop counter variable
int index;
//Accumulator variable initialized to 0
int total = 0;
//Pull in the numbers from the main method to calculate the total
for(index = 0; index < numbers.length; index++)
{
total = total + number[index];
}
return total;
}
//Method getAverage
public static int getAverage(int[] array)
{
//Loop counter variable
int index;
//Pull in the sum and the numbers from the main method to calculate the average
for(index = 0; index < numbers.length; index++)
{
average = sum / number[index];
}
return average;
}
}
答案 0 :(得分:1)
我能看到的第一个问题是,在所有方法中,你从未使用过这些参数。您使用了不同的数组,这些数组在这些方法中不存在。
第二个问题是你试图从一个方法返回两个值。您只能从方法返回一个值。所以你必须摆脱“return highest;
”并复制没有“return lowest;
”的方法,并在需要的地方使用。
我看到的第三件事(虽然没有造成任何错误)是你可以通过说
来缩短代码int total = 0;
for(int index = 0; index < numbers.length; index++)
{
total += number[index];
}
而不是
int index;
int total = 0;
for(index = 0; index < numbers.length; index++)
{
total = total + number[index];
}
答案 1 :(得分:0)
对于初学者,下面的代码试图将变量初始化为不存在的数组值......这些应该完全删除。
//Declare variables to hold the lowest and highest
int lowest = numbers[0];
int highest = numbers[0];
下面的代码中也会出现这些错误,因为您没有将它们传递给函数,因此它在此范围内不存在。 public static int getLowest(int [] array) { for(int i = 1; i&lt; numbers.length; i ++) { if(numbers [i]&gt;最高) 最高=数字[i]; 否则如果(数字[i] <最低) 最低=数字[i]; }
return highest;
return lowest;
}
您还为一个只调用一个的函数提供了2个参数。见下文:
//Call a method to calculate the average of the numbers
average = getAverage(sum, numbers);
public static int getLowest(int[] array)
{
for(int i=1; i< numbers.length; i++)
{
if(numbers[i] > highest)
highest = numbers[i];
else if (numbers[i] < lowest)
lowest = numbers [i];
}
return highest;
return lowest;
}
答案 2 :(得分:0)
我认为这更干净:
public class Main {
public static final int SIZE = 20;
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
List<Integer> numbers = new ArrayList<Integer>();
Double total = 0d;
System.out.println("This program gets a list of 20 numbers. Then displays:");
System.out.println(" the lowest number, the highest number, the total of the numbers, and the average.");
// Get the numbers from the user
System.out.print("Please enter 20 numbers, each seperated by a space: ");
for (int i = 0; i < SIZE; i++) {
Integer currInput = input.nextInt();
numbers.add(currInput);
total += currInput;
}
System.out.println("The lowest number is: " + Collections.min(numbers));
System.out.println("The highest number is: " + Collections.max(numbers));
System.out.println("The total of these numbers are: " + total);
System.out.println("The average of these numbers are: " + (total / SIZE));
}
}
希望它有所帮助:]