gpt4 book ai didi

java - 数字分析程序

转载 作者:行者123 更新时间:2023-12-01 13:59:34 25 4
gpt4 key购买 nike

我正在参加 Java 初学者类(class),刚刚学习数组的概念。我们必须使用注释来表示代码。

我正在尝试编写一个程序,要求用户输入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;
}
}

最佳答案

我看到的第一个问题是,在所有方法中,您从未使用过参数。您使用了不同的数组,该数组不存在于这些方法中。

第二个问题是您试图从一个方法返回两个值。一种方法只能返回一个值。因此,您必须去掉“return最高;”并复制没有“returnlowest;”的方法,并在需要的地方使用每个方法。

我看到的第三件事(尽管它不会导致任何错误)是您可以通过说来缩短代码

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];
}

关于java - 数字分析程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19417181/

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