作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我是一名 Java 新手,我的代码中出现了一个非常奇怪的问题。目标是创建一个包含 20 个随机数的数组列表,显示该数组并根据数组中的数据执行一系列计算。这必须通过 6 种方法来完成。除了最后一个方法之外,一切似乎都工作正常。它的目的是获取数组中的最小数字,但由于某种原因它只输出 0。奇怪的是,对于上面的方法来说,它工作得很好,它收集了数组中的最大数字。
我尝试对其进行故障排除,看看它是否可能来自具有空值的数组,但这不太可能,因为最高的方法似乎工作正常。最低的列表靠近底部。
感谢您的帮助,非常感谢这个新手。另外,如果您有任何其他很棒的评论,我愿意接受批评!
import java.util.Random;
public class Program7 {
public static void main(String[] args) {
final int listSize = 20; // List size
double total = 0; // Total
int[] ranList = new int[listSize]; // Array with random numbers
int highest = ranList[0]; // Highest variable
int lowest = ranList[0]; // Lowest Variable
randomNumberList(ranList); // Calls randomNumberList method
displayList(ranList); // Calls displayList method
total = totalList(total, ranList); // Calls totalList method
double averageList = listAverage(total, ranList); // Calls averageList method
highest = listHighest(ranList, highest); // Calls listHighest method
lowest = lowestList(ranList, lowest); // Calls lowestList method
// On screen output
System.out.println("\nHere's the total: " + total);
System.out.println("Here's the average: " + averageList);
System.out.println("Here's the highest number in the list: " + highest);
System.out.println("Here's the lowest number in the list: " + lowest);
}
/**
* Generates a random list
*
* @param ranList
*/
public static void randomNumberList(int[] ranList) {
for (int i = 0; i < ranList.length; i++) {
Random generator = new Random();
int ranNum = generator.nextInt(100) + 1;
ranList[i] = ranNum;
}
}
/**
* Displays list
*
* @param ranList
*/
public static void displayList(int[] ranList) {
System.out.println("Here is your random list: ");
for (int i = 0; i < ranList.length; i++) {
System.out.print(ranList[i] + " ");
}
}
/**
* Adds elements in list
*
* @param total
* @param ranList
* @return returns total of list added together
*/
public static double totalList(double total, int[] ranList) {
for (int i = 0; i < ranList.length; i++) {
total += ranList[i];
}
return total;
}
/**
* Finds average by dividing the total with the ranList.length
*
* @param total
* @param ranList
* @return result of the averaging
*/
public static double listAverage(double total, int[] ranList) {
double averageList = total / ranList.length;
return averageList;
}
/**
* Steps through array via loop and finds highest value
*
* @param ranList
* @param highest
* @return the highest value
*/
public static int listHighest(int[] ranList, int highest) {
for (int i = 0; i < ranList.length; i++) {
if (ranList[i] > highest) {
highest = ranList[i];
}
}
return highest;
}
/**
* Steps through array via loop and finds lowest value
*
* @param ranList
* @param lowest
* @return the lowest value
*/
public static int lowestList(int[] ranList, int lowest) {
for (int i = 0; i < ranList.length; i++) {
if (ranList[i] < lowest) {
lowest = ranList[i];
}
}
return lowest;
}
}
最佳答案
在将其传递到方法之前,您将其设置为 0。
int[] ranList = new int[listSize]; // Array with random numbers (not yet, right now this is an array of all 0)
int highest = ranList[0]; // Highest variable (not really, this is 0)
int lowest = ranList[0]; // Since you didn't put anything in ranList, lowest is now 0
对于您的最高函数,这工作得很好,因为您的随机数始终高于 0。
实际上,这里的根本原因是您根本不应该将任何内容传递给这些方法,因为它们没有执行任何操作。
public static int lowestList(int[] ranList) {
int lowest = Integer.MAX_VALUE;
for (int i = 0; i < ranList.length; i++){
if (ranList[i] < lowest){
lowest = ranList[i];
}
}
return lowest;
}
关于java低值输出0,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15280441/
我是一名优秀的程序员,十分优秀!