Java阵列的最低数量和平均数量无法正确计算 [英] Java array lowest number and average not computing correctly
问题描述
我是Java新手.我的作业需要以下条件:开发一个程序,允许用户将数字输入到数组中.输入如下:
I am new to java. I have an assignment that requires the following: Develop a program which allows the user to enter numbers into an array. Input will be as follows:
用户将输入要输入到数组中的整数总数.
The user will enter the total number of integers to be entered into the array.
然后,用户将输入该数量的唯一整数(负数或正数).不允许输入的值数超过数组大小.
The user will then enter that number of unique integers (negative or positive). Do not allow the number of values entered to exceed the array size.
开发方法以:
排序数组
确定最大值
确定最小值
计算平均值(两倍)
下面的代码可以运行,但是它将我输入的最低数字显示为0,这是不正确的,并且平均值的确以双精度格式显示,但是始终将数字四舍五入(3.0而不是3.3).有人可以让我知道我在做什么错以及如何解决这个问题.我已经搜寻问题三天了,却找不到我哪里出了错.
The code below does run but it displays the lowest number I enter as 0, which is not correct and the average does display in a double format but it always rounds the number (3.0 instead of 3.3). Can someone please let me know what I am doing wrong and how can I fix this. I have been googling the problem for 3 days now and can't find out where I went wrong.
import java.util.Scanner;
import java.util.Arrays;
public class UserArray2 {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
//allow user input;
System.out.println("How many numbers do you want to enter?");
int num = input.nextInt();
int numbers[] = new int[num];
int low = numbers[0];
int max = numbers[0];
int sum = 0;
double avgNum;
System.out.println("Enter the " + num + " numbers now.");
for (int i = 0 ; i < numbers.length; i++ ) {
numbers[i] = input.nextInt();
}
//sort
System.out.println("The numbers you entered have been sorted from lowest to greatest:");
Arrays.sort( numbers );
for ( int i = 0 ; i < numbers.length ; i++ ) {
System.out.println(numbers[i]);
}
//max
for (int counter = 1; counter < numbers.length; counter++)
{
if (numbers[counter] > max)
{
max = numbers[counter];
}
}
//lowest
for (int counter = 1; counter < numbers.length; counter++)
{
if (numbers[counter] < low)
{
low = numbers[counter];
}
}
//average
for (int i : numbers)
sum += i;
avgNum = sum/numbers.length;
System.out.println("The largest number on your list is: " + max);
System.out.println("The lowest number on your list is: " + low);
System.out.println("The average of the list of numbers is: " + avgNum);
}
public static void printArray(int arr[]){
int n = arr.length;
for (int i = 0; i < n; i++) {
System.out.print(arr[i] + " ");
}
}
}
推荐答案
您非常亲密.以下是导致您的代码无法正确完成的问题:
You are very close. Here are the problems that prevent your code from completing correctly:
-
您过早初始化
min
和max
.对于min
,当所有数字均为正数时,这会产生问题.对于最大值,当所有数字均为负数时.您应该在读取完成后 声明/初始化两个变量.将声明移至读取循环后的位置即可解决此问题.
You initialize
min
andmax
too soon. Formin
this creates problems when all numbers are positive; for max, when all numbers are negative. You should declare/initialize both variables after the reading has been completed. Moving the declaration to the point after the reading loop will fix this problem.
尽管您已将 avgNum
的类型正确定义为 double
,但您为其分配的表达式仍为 int
,因为两者它的元素是 int
.因此,该划分被截断.如下更改分区:
Although you correctly defined the type of avgNum
as double
, the expression that you assign it remains an int
, because both its elements are int
. Therefore, the division is truncated. Change the division as follows:
avgNum = (double)sum/numbers.length;
或更妙的是,将 sum
声明为 double
,然后删除演员表.
or better yet, declare sum
as double
, and remove the cast.
这篇关于Java阵列的最低数量和平均数量无法正确计算的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!