0

我在让我的方法工作时遇到了一些麻烦,我相信我在调用其他方法的主要方法中做错了。我不确定是否需要 if、while 或 for 语句。谁能帮忙,我真的很感激。这是我的程序...

  public static void main(String[] args) throws IOException {
    Scanner kb = new Scanner(System.in);
    final int MAX = 100;
    int [] myarray = new int [MAX];
    int fillsize, total = 0, num;
    int smallest = 0, largest = 0;
    fillsize = fillarray (myarray, MAX);
    printarray (myarray, fillsize);

    num = kb.nextInt(); 
    int small = num;
    int large = num;
    total = Average(total, num);
    if(smallest (num, smallest))
    {
      small = num;
    }
    if(largest(num, largest))
    {
       large = num;      
    }
    System.out.println("The smallest value is: " + smallest);
    System.out.println("The largest value is: " + largest);
    System.out.println("The average is: " + total);

    prw.close();
}

public static int fillarray (int[] num, int MYMAX){
    Random gen = new Random();
    int retval = 0;
    int randomnum;
    for(int count = 0; count <= 30; count++){
        randomnum = gen.nextInt(150);
        num [count] = randomnum;
        System.out.println(randomnum);
        retval++;   
    }
    System.out.println("The return value is: " + retval);
    return (retval);
}
public static void printarray (int[] num, int fillsize){
    for (int counts = 0; counts < fillsize; counts++){
        System.out.println("For the position ["+counts+"] the value is " + num[counts]);
    }
    return;
}
public static boolean smallest (int num1, int num2){
    boolean returnValue;
    if (num2 < num1){
        returnValue = true;
    }
    else {
        returnValue = false;
    }
    return (returnValue);
}
public static boolean largest (int number1, int number2){
    boolean returnVal;
    if (number1 > number2){
        returnVal = true;
    }
    else{
        returnVal = false;
    }
    return (returnVal);
}
public static int Average (int avg, int sum){
    int retVal;
    retVal = avg + sum;
    return(retVal);
}

我究竟做错了什么?

4

1 回答 1

1

我怀疑问题是您将最小和最大都设置为 num ,这会使您的 if 多余。

尝试将它们都设置为 0。

此外,不推荐使用同名的变量和方法。(令人困惑)。

于 2013-04-12T21:49:00.543 回答