我目前正在为我的计算机访问课程做作业。下面,我为上下文的项目截图。我们的老师给了我们代码片段,让我们开始。在这种情况下,他给了我们代码,该代码填充了一个用户的2个数组。我编写了在数组中找到最小数字(以计算范围)的代码,它可以工作。如下所示...
上下文:https://i.imgur.com/sReC8ym.png
public static void main(String[] args) {
double[] values = {81, 52, 10, 50, 18, 4, 7};
double smallest = values[0];
for (int i = 0; i < values.length; i++) {
if (values[i] < smallest) {
smallest = values[i];
}
}
System.out.println(smallest);
}
但是,当我在项目中实现此代码时,结果始终为0。我认为问题出在代码段。查找最高编号的功能在项目内起作用。找到最大的数字并让最小的数字起作用,但是最小的数字结果始终为0。我将整个程序粘贴到下面。抱歉,如果要解决的工作太多,您有什么建议吗?谢谢
public static String[] strings;
public static double[] data;
public static void main(String[] args) {
populatArrays();
printArrays(strings, data);
double theAverage = calculateAverage(data);
System.out.println("The average rainfall is " + theAverage);
double largest = getLargest(data);
System.out.println("The largest amount of rainfall is " + largest);
double smallest = getSmallest(data);
System.out.println("The smallest amount of rainfall is " + smallest);
double theRange = getRange(data);
System.out.println("The rainfall range is " + theRange);
double theTotal = getTotal(data);
System.out.println("The total rainfall is " + theTotal);
}
//User input for the array & printing out questions
public static void populatArrays() {
Scanner input = new Scanner(System.in);
System.out.println("How many cities would you like to add to the Rainfall calculator?");
int elements = input.nextInt();
String[] someStrings = new String[elements];
double[] someData = new double[elements];
for (int i = 0; i < someData.length; i++) {
input = new Scanner(System.in);
System.out.println("Enter a City");
someStrings[i] = input.nextLine();
System.out.println("Enter the annual rainfall");
someData[i] = input.nextDouble();
}
strings = someStrings;
data = someData;
}
//code printing out the array on screen
public static void printArrays(String[] stringArray, double[] doubleArray) {
for (int i = 0; i < stringArray.length; i++) {
System.out.println("City: " + stringArray[i]);
System.out.println(doubleArray[i] + "mm");
}
}
//code calculating the mean average
public static double calculateAverage(double[] thenumbers) {
double sum = 0;
for (double thenumber : thenumbers) {
sum += thenumber;
}
double result = sum / thenumbers.length;
return result;
}
//code calculating the largest number in the array
public static double getLargest(double[] thenumbers) {
double largest = 0;
for (int i = 0; i < thenumbers.length; i++) {
if (thenumbers[i] > largest) {
largest = thenumbers[i];
}
}
return largest;
}
//code calculating the smallest number in the array
public static double getSmallest(double[] thenumbers) {
double smallest = 0;
for (int i = 0; i < thenumbers.length; i++) {
if (thenumbers[i] < smallest) {
smallest = thenumbers[i];
}
}
return smallest;
}
// code calculating the range
public static double getRange(double[] thenumbers) {
double largest = getLargest(data);
double smallest = getSmallest(data);
double theRange = largest + smallest;
return theRange;
}
// code calculating the total rainfall in the array
public static double getTotal(double[] thenumbers) {
double sum = 0;
for (double thenumber : thenumbers) {
sum += thenumber;
}
double theTotal = sum;
return theTotal;
}
}
答案 0 :(得分:1)
您正在将最小值的初始值设置为0
由于所有数据都不小于0,因此最小数据永远不会设置为另一个数字。
建议不要将值设置为零,而应将其设置为数组的第一个值,而不是将其设置为原始代码段
double smallest = thenumbers[0];
您在getLargest函数中也进行了同样的操作,因此,如果列表中的最高数字为负数,则将返回0。
作为额外的好处,您也可以使用for in / each运算符:
//code calculating the smallest number in the array
public static double getSmallest(double[] thenumbers) {
double smallest = thenumbers[0];
for (double num : thenumbers) {
if (num < smallest) {
smallest = num;
}
}
return smallest;
}
答案 1 :(得分:1)
您不应使用
double smallest = 0;
这意味着除非数组中的数字为负数,否则此比较将永远不会成功。
if (thenumbers[i] < smallest)
您应该做的是使用
进行初始化 double smallest = Double.MAX_VALUE
这意味着与数组中的任何其他数字相比,您的条件将至少通过一次。另一种选择是使用数组中的任意数字(例如第一个数字)对其进行初始化。
double smallest = thenumbers[0]
然后从那里继续循环,您可以将循环更改为
for (int i = 1; i < thenumbers.length; i++) {
跳过第一个值与其自身的无用比较。
这假设您在数组中至少有一个值,这并不总是一个安全的假设,但对于像这样的琐碎示例问题可能还可以。