将用户输入的数字限制在1到30之间

时间:2015-03-31 23:44:21

标签: java

我正在编写一个程序,要求输入5个用户的数字;但是,我需要将这些数字限制在1到30之间。我在为程序的这一部分编写while循环时遇到了麻烦。

这是我的代码:

 Scanner input = new Scanner(System.in); 

         int numberCounter = 0; // Set scanner to 0
         // Intialize numbers 1-5
         int number1; 
         int number2;
         int number3;
         int number4;
         int number5;
         String asterisk = "*"; // Holds the string value "*"

 int number = 0;
             while (number < 1 || number > 30){
                 System.out.print("Enter the first number (between 1-30): ");
                 number = keyin.nextInt();
             }

         System.out.print("Please enter a number between 1 and 30: ");// Calls for user input
         number1 = input.nextInt();// Stores user input for number 1

         System.out.print("Please enter a number between 1 and 30: ");// Calls for user input
         number2 = input.nextInt();// Stores user input for number 2

         System.out.print("Please enter a number between 1 and 30: ");// Calls for user input
         number3 = input.nextInt();// Stores user input for number 3

         System.out.print("Please enter a number between 1 and 30: ");// Calls for user input
         number4 = input.nextInt();// Stores user input for number 4

         System.out.print("Please enter a number between 1 and 30: ");// Calls for user input
         number5 = input.nextInt();// Stores user input for number 5

         System.out.printf("%d ", number1);
         for (int i = 1; i <= number1; i++)
             System.out.print("*");
         System.out.println();

         System.out.printf("%d ", number2);
         for (int i = 1; i <= number2; i++)
             System.out.print("*");
         System.out.println();

         System.out.printf("%d ", number3);
         for (int i = 1; i <= number3; i++)
             System.out.print("*");
         System.out.println();

         System.out.printf("%d ", number4);
         for (int i = 1; i <= number4; i++)
             System.out.print("*");
         System.out.println();

         System.out.printf("%d ", number5);
         for (int i = 1; i <= number5; i++)
             System.out.print("*");
         System.out.println();

     }

 }

任何人都可以帮助引导我朝着正确的方向前进。

谢谢。

3 个答案:

答案 0 :(得分:1)

您可以通过在循环中保持nextInt()并使用计数器来跟踪您想要读取的所需输入数量来避免冗余代码。并在读取所有有效数字时退出循环。

    Scanner input = new Scanner(System.in);
    byte maxNum = 31;
    byte minNum = 1;

    int[] numbers = new int[5];

    int number = 0;
    int i = 0;

    while (i<5) {

        System.out.print("Enter number("+(i+1)+") between "+minNum + " and "+maxNum+ " : ");
        number = input.nextInt();

        if(number >=minNum && number <= maxNum){
            numbers[i] = number;
            i++;
        }else{
            System.out.println("--------Invalid Number :"+number+ " ");
        }
    }

    System.out.print("Entered Numbers :");

    for(int n : numbers){
        System.out.print(n + ",");
    }

注意:对于char和超出输入范围,代码可能会失败。

答案 1 :(得分:0)

试试这个:

if(Math.max(new int[]{number1,number2,number3,number4,number5}) <= 30 && Math.min(new int[]{number1,number2,number3,number4,number5}) >= 1){
    ...WITHIN YOUR RANGE...
}else{

}

这样做是检查集合的最大值是否小于或等于30,并且集合的最小值大于或等于1,然后执行代码。

答案 2 :(得分:0)

如果你的这个程序的目的是要求用户输入1到30之间的5个数字,我认为你应该为每个输入任务编写'while'循环,以确保每个数字在该范围内并询问用户再次,如果不是。