Java根据此输入,程序将显示小于50的整数

时间:2018-11-15 10:40:59

标签: java arrays parameters

对于该任务,我需要编写一个Java应用程序,该应用程序接受来自用户的30个整数。输入应在1-200的范围内。如果用户输入的输入不在此范围内,则需要显示错误消息。基于此输入,程序将显示在以下类别中输入的整数数量: 少于50 50至100(包括50至100)之间

样本输出: 输入数字4:211

输入错误。请仅输入1到200之间的数字

输入数字4:20

..

..

输入数字30:90

少于50:12

50-100(包括50和100)之间:8

101-150(包括101和150)之间:5

import java.util.Scanner;

public class Main {

    public static void main(String[] args) {
        // Keyboard Initialization
        Scanner kbin = new Scanner(System.in);

        // a.Declare an array to hold 5 Integer values
        int list[] = new int[5];
        int i = 0;

        System.out.print("\n\tInput numbers from 1 to 200: \n");
        while (i < 5) {
            // b.Fill the array with intgers from the keyboard(range: 1 to 200).
            System.out.print("Enter Integer" + (i + 1) + ":");
            int value = kbin.nextInt();

            if (value >= 1 && value <= 200) {
                list[i] = value;
                i++;
            } else {
                System.out.println("!! Error! Please Enter Value between 1 and 200 !!");
            }
        }

    }
}

2 个答案:

答案 0 :(得分:-1)

@Dilip是一个非常简单的问题。只需引入一个flag变量即可标记输入是否错误。如果是,则再次接受该值。否则继续。 以下是输入部分的代码段,其中还考虑了正确和错误的输入边界。

        System.out.print("\n\tInput numbers from 1 to 200: \n");
        for(i = 0; i < 5; i++) {
          flag = 0;
          while(flag == 0) {
            list[i] = kbin.nextInt();
            if(!(list[i] < 1 || list[i] > 200)){
              flag = 1;
            }
            else {
              System.out.println("INVALID INPUT!!! Enter a valid number.");
            }
          }
        }

我已经仔细阅读了您在评论部分中提到的问题说明,并编写了可以达到目的的程序。我还附上了输出,以供确认和参考。希望这会有所帮助。

代码:

import java.util.Scanner;

public class Main {

    public static void main(String[] args) {
        // Keyboard Initialization
        Scanner kbin = new Scanner(System.in);

        // a.Declare an array to hold 5 Integer values
        int list[] = new int[5];
        int category1[] = new int[5], k1 = 0;
        int category2[] = new int[5], k2 = 0;
        int category3[] = new int[5], k3 = 0;
        int i, flag;

        System.out.print("\n\tInput numbers from 1 to 200: \n");
        for(i = 0; i < 5; i++) {
          flag = 0;
          while(flag == 0) {
            System.out.print("Enter the input number " + (i+1) + ": ");
            list[i] = kbin.nextInt();
            if(!(list[i] < 1 || list[i] > 200)){
              flag = 1;
            }
            else {
              System.out.println("INVALID INPUT!!! Enter a valid number.");
            }
          }
        }

        for(i = 0; i < 5; i++) {
          if(list[i] < 50)
            category1[k1++] = list[i];
          else if(list[i] < 101)
            category2[k2++] = list[i];
          else
            category3[k3++] = list[i];
        }

        System.out.print("Category 1(1 TO 49): ");System.out.println(k1);
    System.out.print("Category 2(50 TO 100): ");
    System.out.println(k2);
    System.out.print("Category 3(greater than 100): ");
    System.out.println(k3);
    System.out.print("Category 4(151 to 200): ");
    System.out.println(k4);
    }
}

输出:

  Input numbers from 1 to 200: 
Enter the input number 1:  2
Enter the input number 2:  3
Enter the input number 3:  50
Enter the input number 4:  56
Enter the input number 5:  159
Category 1(1 TO 49): 2
Category 2(50 TO 100): 2
Category 3(101 to 150): 0
Category 4(151 to 200): 1

答案 1 :(得分:-1)

这里是使用Java Streams的示例。在这里,我们只是简单地先读取所有值,然后再过滤掉每个类别。从性能角度来看,如果您不关心实际计数,最好对每个类别使用一个计数器。

public class Test {
  public static void main(String[] args) {
    int counter = 0;
    Scanner scanner = new Scanner(System.in);
    List<Integer> values = new ArrayList<>();

    while (counter < 5) {
      System.out.print("Enter integer (" + (counter + 1) + "): ");
      int value = scanner.nextInt();

      if (value >= 1 && value <= 200) {
        counter++;
        values.add(value);
      } else {
        System.out.println("Please enter a value between 1 and 200");
      }
  }
  System.out.println("Between 1-50   : " + values.stream().filter(val -> val < 50).count());
  System.out.println("Between 50-100 : " + values.stream().filter(val -> val >= 50 && val <= 100).count());
  System.out.println("Between 101-150: " + values.stream().filter(val -> val > 100 && val <= 150).count());
  System.out.println("Between 151-200: " + values.stream().filter(val -> val > 150 && val <= 200).count());
}

输出示例:

Enter integer (1): 5
Enter integer (2): 55
Enter integer (3): 125
Enter integer (4): 175
Enter integer (5): -2
Please enter a value between 1 and 200
Enter integer (5): 201
Please enter a value between 1 and 200
Enter integer (5): 199
Between 1-50   : 1
Between 50-100 : 1
Between 101-150: 1
Between 151-200: 2