java程序中的方法?

时间:2014-04-03 00:29:14

标签: java

有人请告诉我如何制作min和max方法吗?我真的不知道,我尝试了很多东西,但我失败了 我只是个初学者 .................................................. ................ 这是我现在的代码

class ArrayCalculator
{
public static void main(String arng[])
{
    int max=0;      // sum of maximum number in array
    int min=0;      // sum of minimum numbers in array
    int total=0;    // sum of total nnymbers in array
    int average=0;  // sum of average number in array

    System.out.println("");
    System.out.println("*****************WELCOME TO ARRAY CALCULATOR****************"); // Introducton to calculater
    System.out.println("****************-----------------------------***************");
    System.out.println("*****************---------------------------****************"); 
    System.out.println("How Many Numbers Would You Like To Calculate?"); // read in numbers entered by user
    int Items = EasyIn.getInt();
    int[] numbers = new int[Items]; // array for numbers entered

    for(int i = 0; i< Items; i++)
    {
        System.out.println("Enter number " + (i+1)); // read in each figure entered by user
        numbers[i]=EasyIn.getInt();
        total=numbers[i];
        total = total + numbers[i];
        min=numbers[0];
        max=numbers[0];

        if (numbers[i] > max) // calculations for maximum number
        {
            max = numbers[i];
        }

        else if (numbers[i] < min) // calculations for minimum number
        {
            min = numbers[i];
        }
    }

    average = total/Items; // calculations for average number
    System.out.println("*********************************************");
    System.out.println("You have entered " +numbers.length+" numbers"); // display count of numbers entered
    System.out.println("Largest number in array is " +max); // display maximum number
    System.out.println("Smallest number in array is " +min); // display minimum number
    System.out.println("Total Numbers is " +total); // display total sum of numbers
    System.out.println("Average is " +average); // display average number
    System.out.println("Thank You");
    System.out.println("*********************************************");
}
}

编者注:上面的代码在下面与使用扫描程序而不是EasyIn重复

import  java.util.Scanner;
    public class ArrayCalculator  {
    public static void main(String arng[])
    {
        Scanner scanr = new Scanner(System.in);
        int max=0;      // sum of maximum number in array
        int min=0;      // sum of minimum numbers in array
        int total=0;    // sum of total nnymbers in array
        int average=0;  // sum of average number in array

    System.out.println("");
    System.out.println("*****************WELCOME TO ARRAY CALCULATOR****************"); // Introducton to calculater
    System.out.println("****************-----------------------------***************");
    System.out.println("*****************---------------------------****************");

        System.out.println("How Many Numbers Would You Like To Calculate?"); // read in numbers entered by user
        int Items = scanr.nextInt();
        scanr.nextLine();
        int[] numbers = new int[Items]; // array for numbers entered

        for(int i = 0; i< Items; i++)
        {
            System.out.println("Enter number " + (i+1)); // read in each figure entered by user
            numbers[i]=scanr.nextInt();
            scanr.nextLine();
            total=numbers[i];
            total = total + numbers[i];
            min=numbers[0];
            max=numbers[0];
            if (numbers[i] > max) // calculations for maximum number
            {
                max = numbers[i];
            }
            else if (numbers[i] < min) // calculations for minimum number
            {
                min = numbers[i];
            }
        }

        average = total/Items; // calculations for average number
        System.out.println("*********************************************");
        System.out.println("You have entered " +numbers.length+" numbers"); // display count of numbers entered
    System.out.println("Largest number in array is " +max); // display maximum number
    System.out.println("Smallest number in array is " +min); // display minimum number
    System.out.println("Total Numbers is " +total); // display total sum of numbers
        System.out.println("Average is " +average); // display average number
        System.out.println("Thank You");
        System.out.println("*********************************************");
    }
}

3 个答案:

答案 0 :(得分:0)

您应该删除这些行。否则,每次设置数字时都会重置total,max和min。

total=numbers[i];
min=numbers[0];
max=numbers[0];

而不是

else if (numbers[i] < min)

你可以简单地写

else

答案 1 :(得分:0)

另一种解决方法如下:

填充numbers后,

List<Integer> list = Arrays.asList(numbers);
int max = Collections.max(list);
int min = Collections.min(list);

只是为了表明java为你做了很多你不必重新发明的东西。

集合中还有更多功能 见http://docs.oracle.com/javase/7/docs/api/java/util/Collections.html

答案 2 :(得分:0)

这是一个从数组中获取最大整数的函数。通过一个简单的更改,您还可以将其变为最小功能。

public static final int getMaxNum(int[] numbers)  {
   int max = Integer.MIN_VALUE;
   for(int i : numbers)  {
      if(i > max)  {
         max = i;
      }
   }
   return  max;
}

调用它
int max = getMaxNum(numbers);