处理数组的Java程序专门发现一个大于限制的值

时间:2019-03-21 00:54:20

标签: java arrays

编写一个执行以下操作的程序:

  1. 要求用户输入10个整数(以填充数组)。
  2. 要求用户输入一个数字,代表一个限制。
  3. 调用一个显示所有大于限制值的方法。

    3a。您的方法应接受数组和限制作为形式参数。

    3b。该方法不返回值,而是输出大于限制值的值(注意:如果限制值太大,则输出可能为空白)。

到目前为止我所做的:

import java.util.scanner;

public class Array {
    public static void main(String[] args) {
        Scanner input = new Scanner(System. in );
        double[] list2 = new double[10];
        System.out.println("Enter 10 double values");
        int i = 0;
        while (i < list2.length) {
            list2[i] = input.nextDouble();
            i++;
        }
        System.out.println("The average is " + average(list2));
    }

    public static double average(double[] array) {
        double sum = 0;
        for (int i = 0; i < array.length; i++)
        sum += array[i];
        return sum / array.length;
    }
}

2 个答案:

答案 0 :(得分:0)

这是一种基本方法:

double[] aboveLimit = new double[list2.length];
double limit; //set limit variable
int j = 0;
for(int i = 0;i < list2.length;i++) {
  if(list2[i] > limit) {
    aboveLimit[j] = list2[i];
    j++;
  }
}

for(int i = 0;i < aboveLimit.length;i++) { //checks when reached end of all numbers
    if(aboveLimit[i] == 0) {
        if(i == 0) {
            break; //if there are no numebrs greater than the limit, nothing happens
        }
        else {
            System.out.println(" are above the limit.");
            break;
        }
    }
    else {
        System.out.println(aboveLimit[i] + ", ");
    }
}

答案 1 :(得分:0)

您只需要创建一个新的array,它将存储所有大于limit的值(如果有)。然后通过计数器j可以确定某些值是否更大,因为计数器j仅在存在一些较大值时才会递增:

工作代码还包括您的一般方法:

import java.util.Scanner;
public class stackArray 
{
    public static void main(String[] args) 
    {
        Scanner input = new Scanner(System. in );
        double[] list2 = new double[10];
        double limit;
        System.out.println("Enter 10 double values");
        int i = 0;
        while (i < list2.length) 
        {
            list2[i] = input.nextDouble();
            i++;
        }
        System.out.println("The average is " + average(list2));
        System.out.print("\nEnter Limit: ");
        limit = input.nextDouble();
        findGreater(list2,limit);
    }

   public static void findGreater(double [] array, double limit)
   {
        double[] greater = new double[array.length];  // new array to save all the greater value
        int j = 0; // index to add value in Greater array
        for(int i = 0;i < array.length;i++) 
        {
            if(array[i] > limit) 
            {
                greater[j] = array[i];
                j++;
            }
        }

        if(j!=0)  //if J!=0 it means there is some value thats why J is incremneted in above FOR Loop
        {           
            System.out.printf("\nValue above %.2f are: ", limit);
            for(int i = 0;i < j;i++) 
            {
                System.out.printf(greater[i] + " ");
            }
        }
        else
        {
            System.out.printf("\nNo vlaue is above %.2f", limit);
        }

   }

    public static double average(double[] array) 
    {
        double sum = 0;
        for (int i = 0; i < array.length; i++)
            sum += array[i];
        return sum / array.length;
    }
}