java低值输出0

java低值输出0,java,Java,我是一个Java新手,我的代码中出现了一个非常奇怪的问题。目标是在一个数组中创建一个包含20个随机数的列表,显示该数组,并根据数组中的数据执行一系列计算。这必须通过6种方法完成。除了最后一种方法外,一切似乎都很好。它的目的是获取数组中的最低数字,但出于某种原因,它只输出一个0。奇怪的是,对于上面的方法,is工作得非常好,它收集了数组中的最高数量 我试着解决它,看看它是否来自一个空值的数组,但这不太可能,因为最高的方法似乎可以正常工作。最下面的靠近底部 谢谢你的帮助,非常感谢这个新手。此外,如果你

我是一个Java新手,我的代码中出现了一个非常奇怪的问题。目标是在一个数组中创建一个包含20个随机数的列表,显示该数组,并根据数组中的数据执行一系列计算。这必须通过6种方法完成。除了最后一种方法外,一切似乎都很好。它的目的是获取数组中的最低数字,但出于某种原因,它只输出一个0。奇怪的是,对于上面的方法,is工作得非常好,它收集了数组中的最高数量

我试着解决它,看看它是否来自一个空值的数组,但这不太可能,因为最高的方法似乎可以正常工作。最下面的靠近底部

谢谢你的帮助,非常感谢这个新手。此外,如果你有任何其他意见,这将是了不起的,我是开放的批评

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;
    }
}
import java.util.Random;
公共课程7{
公共静态void main(字符串[]args){
final int listSize=20;//列表大小
双倍总计=0;//总计
int[]ranList=new int[listSize];//带有随机数的数组
int highest=ranList[0];//highest变量
int lowest=ranList[0];//最低的变量
randomNumberList(ranList);//调用randomNumberList方法
displayList(ranList);//调用displayList方法
total=totalList(total,ranList);//调用totalList方法
double averageList=listAverage(total,ranList);//调用averageList方法
highest=listHighest(ranList,highest);//调用listHighest方法
lowest=lowestList(ranList,lowest);//调用lowestList方法
//屏幕输出
System.out.println(“\n是总数:“+total”);
System.out.println(“这里是平均值:“+averageList”);
System.out.println(“这里是列表中的最大数字:“+highest”);
System.out.println(“这是列表中最低的数字:“+lowest”);
}
/**
*生成一个随机列表
*
*@param-ranList
*/
公共静态void randomNumberList(int[]ranList){
for(int i=0;i最高){
最高=等级列表[i];
}
}
回报最高;
}
/**
*通过循环遍历数组并找到最小值
*
*@param-ranList
*@param最低
*@返回最低值
*/
公共静态整数最低列表(整数[]ranList,整数最低){
for(int i=0;i
创建随机数组后,应初始化最高和最低。它们都是从0开始的。

在将其传递到方法之前,将最低值设置为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;
}
publicstaticint最下面的列表(int[]ranList){
int lowest=整数最大值;
for(int i=0;i
尝试将

lowest = ranList[0];
在您生成列表后,即在您呼叫

randomNumberList(ranList);