从阵列中找到最小的。以及偶数和奇数的平均值?

时间:2016-08-31 12:56:12

标签: java arrays

package lesson5;

import java.util.Scanner;

public class task55 {

private static Scanner sc;

public static void main(String[] args) {
    sc = new Scanner(System.in);
    int i = 0;
    double num[] = new double[5];
    double min_even = 0;
    double evencount = 0;
    double evensum = 0;
    int oddcount = 0;
    int oddsum = 0;

    for (i = 0; i < 5; i++) {
        System.out.println("input number");
        num[i] = sc.nextInt();
    }

    System.out.println(" print even num : ");

    for (i = 0; i < 5; i++) {
        if (num[i] % 2 == 0) {

            evencount++;
            evensum += num[i];
            System.out.println(num[i] + " ;");
            if (min_even > num[i])
                min_even = num[i];

        }
    }

    System.out.println(" evencount = " + evencount);
    System.out.println(" evensum =" + evensum);
    System.out.println(" ceven average = " + evensum / evencount);
    System.out.println("-------------------------------");
    System.out.println(" print odd num :");

    for (i = 0; i < 5; i++) {
        if (num[i] % 2 != 0) {
            oddcount++;
            oddsum += num[i];
            System.out.println(num[i] + " ;");

        }
    }

    System.out.println(" oddcount " + oddcount);
    System.out.println(" oddsum =" + oddsum);
    System.out.println(" average odd = " + oddsum / oddcount);

}

}

上面提到的代码有效。只是我无法获得结果最小值和最大值。 请帮我打印min_even和max_even以及min_odd和Max_odd。

2 个答案:

答案 0 :(得分:0)

试试这个。

package lesson5;
import java.util.Scanner;

public class task3 {
  public static void main(String[] args) {
    Scanner sc = new Scanner(System.in);
    int [] masarray = new int [10];
    int a,b,c,d,f,g,j,m,n,l;

    int flag_even =0;
    int min_even = 0;
    float sum_even=0;
    float sum_odd=0;
    int total_even=0;
    int total_odd=0;

    System.out.println("input first  number of array");
    a=sc.nextInt();
    masarray [0]= a;

    System.out.println("input second  number of array");
    b=sc.nextInt();
    masarray [1]= b;

    System.out.println("input third  number of array");
    c=sc.nextInt();
    masarray [2]= c;

    System.out.println("input fourth  number of array");
    d=sc.nextInt();
    masarray [3]= d;

    System.out.println("input fiveth  number of array");
    f=sc.nextInt();
    masarray [4]= f;

    System.out.println("input sixth  number of array");
    g=sc.nextInt();
    masarray [5]= g;

    System.out.println("input seventh  number of array");
    j=sc.nextInt();
    masarray [6]= j;

    System.out.println("input eighth  number of array");
    m=sc.nextInt();
    masarray [7]= m;

    System.out.println("input nineth number of array");
    n=sc.nextInt();
    masarray [8]= n;

    System.out.println("input tenth  number of array");
    l=sc.nextInt();
    masarray [9]= l;

    for ( int i =0; i < masarray.length; i++){
        if (masarray[i]%2==0){

            if(flag_even==0)//Assigning first even number found in array to min_even to initialize it
            {
                min_even=masarray[i];
                flag_even=1;//Initialization done
            }

            else if(min_even>masarray[i])
                min_even=masarray[i];

            sum_even+=masarray[i];//calculates sum of all even numbers
            System.out.println("even number = " + masarray[i]); 

            total_even++;//counting total number of even numbers
        }

        else
        {
            sum_odd=masarray[i];//calculates sum of all odd numbers
            total_odd++;//counting total number of odd numbers
        }
    }

    if(flag_even==0)
        System.out.println("No even number present in array!");
    else
    {   
        System.out.println("Minimum Even number in given array is:"+min_even);
        System.out.println("Average of Even numbers in given array is:"+(sum_even/total_even));
    }

    if(total_odd==0)
        System.out.println("No odd number present in array!");
    else
        System.out.println("Average of Odd number in given array is:"+(sum_odd/total_odd));

  }
}

答案 1 :(得分:-1)

创建两个arrays,一个用于奇数,一个用于偶数,并从底部for循环中推送它们。然后使用this查找每个中最小和最大的数字。