无法检查用户输入是否小于某个数字

时间:2017-07-17 19:26:06

标签: java

当我输入小于12的数字时没有问题但是当我输入更大的数字时,我得到“输入小于13的数字”并且总是在后续输入中得到它。如何使程序检查int和小于13的数字?

package com.company;

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner s = new Scanner(System.in);
        Scanner z = new Scanner(System.in);
        boolean cont;
        boolean isNumber;

        do {
            System.out.println("Enter the number you want to see the factorial of (0-12)");
            do{
                if(s.hasNextInt()){
                    isNumber = true;
                }else{
                    isNumber=false;
                    System.out.println("Enter a number");
                    s.next();
                }
            }while(!isNumber);
            int input = s.nextInt();

            boolean isSmall;
            do{
                if(input < 13){
                    isSmall= true;

                }else{
                    isSmall=false;
                    System.out.println("Enter a number less than 13");
                    s.next();
                }
            }while(!isSmall);

            int factorial = 1;
            System.out.print(input + "!" + " = ");
            for ( int i =1 ; i<=input ;  i++) {
                factorial *= i;
                System.out.print(i);
                if (input == i) {
                    break;
                }

                System.out.print(" * ");
            }
            System.out.println(" = " + factorial);

            System.out.println("Do you want to continue?");
            String line = z.nextLine();
            if(line.matches("y|YES|Y|yes|Yes")){
                cont=true;
            }else{
                cont= false;
                System.out.println("Thanks for using the program!");
            }
        }while(cont);
    }
}

3 个答案:

答案 0 :(得分:1)

您忘记将新值输入到输入中,因此您总是将值更大13

do{
    if(input < 13){
         isSmall= true;
     }else{
           isSmall=false;
           System.out.println("Enter a number less than 13");
           input =  s.nextInt();
      }
  }while(!isSmall);

答案 1 :(得分:0)

您需要在给出警告后将输入设置为输入的数字。现在,你只是不断循环,因为无论输入什么,输入都保持不变。

        boolean isSmall;
        do {
            if (input < 13) {
                isSmall = true;

            } else {
                isSmall = false;
                System.out.println("Enter a number less than 13");
                input = s.nextInt();

            }
        } while (!isSmall);

答案 2 :(得分:0)

在再次进行比较之前,您没有在第二个do-while循环中读取下一个int。我建议创建一个单独的方法来检索有效数字。

private static int getValidInt(Scanner s) {
    System.out.println("Enter the number you want to see the factorial of (0-12)");

    int input = getInt(s);
    while (input < 0 || input > 12) {
        System.out.println("Enter an integer between 0 and 12.");
        s.next();
        input = getInt(s);
    }
    return input;
}

private static int getInt(Scanner s) {
    while (!s.hasNextInt()) {
        System.out.println("Enter a number");
        s.next();
    }

    return s.nextInt();
}

然后你可以移除你的2个内部do-while循环并用int input = getValidInt(s);

替换它

这也将确保输入始终是一个大于0的int,尽管消息将其作为一项要求,但仍然缺少代码。