【问题标题】:How to make a program prompt the user continuously?如何让程序不断提示用户?
【发布时间】:2017-09-12 09:32:46
【问题描述】:
public class child {
    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        int AGE;
        System.out.println("Enter Child's number(s) of Days: ");
        AGE = scan.nextInt();

        if (AGE == 0 || AGE == 1){
            System.out.println("Classification: New Born");
        }
        else if (AGE >= 2 && AGE <= 10){
            System.out.println("Classification: Infant");
        }
        else if (AGE > 9 && AGE < 19){
            System.out.println("Classification: New Born");
        }
        else if (AGE > 17 && AGE < 37){
            System.out.println("Classification: TODDLER");
        }
        else if (AGE > 36 && AGE < 120 ) {
            System.out.println("Classification: KID");
        }
        else{
            System.out.println("Classification: Out of Range");
            System.out.println("Enter a number again:");
            AGE = scan.nextInt();
            if (AGE == 0 || AGE == 1){
                System.out.println("Classification: New Born");
            }
            else if (AGE >= 2 && AGE <= 10){
                System.out.println("Classification: Infant");
            }
            else if (AGE > 9 && AGE < 19){
                System.out.println("Classification: New Born");
            }
            else if (AGE > 17 && AGE < 37){
                System.out.println("Classification: TODDLER");
            }
            else if (AGE > 36 && AGE < 120 ) {
                System.out.println("Classification: KID");
            }           
            else{
                System.out.println("Classification: Out of Range");
                System.out.println("Enter a number again:");
                AGE = scan.nextInt();
            }   
        }   
    }
}

所以我根据孩子的天数对他们进行了分类。

如何简化此代码,因为如果用户输入超出范围的数字,程序将提示用户输入一个值,但如果用户再次输入超出范围的数字,程序现在将停止提示用户输入另一个数字,因为如果我在 else 下放置另一个条件构造,它将使我的代码更长,所以我的问题是如何使我的代码更短,但如果输入的数字是,程序将继续提示用户超出范围。

示例输出 输入孩子的天数: 121 分类:超出范围 再次输入一个数字: // 程序会再次提示用户 -1 //但是现在程序现在不会打印超出范围 /如何让程序提示用户再次输入数字并制作 我的程序更短/

【问题讨论】:

  • 使用while 循环。
  • 使用while 循环

标签: java


【解决方案1】:

您应该熟悉 while 循环。

只要条件在每次迭代后保持为真,它就会循环。

Scanner scan = new Scanner(System.in);

while(true) {
    System.out.println("Enter Child's number(s) of Days: ");
    int AGE = scan.nextInt();

    if (AGE == 0 || AGE == 1) {
        System.out.println("Classification: New Born");
    } else if (AGE >= 2 && AGE <= 10) {
        System.out.println("Classification: Infant");
    } else if (AGE > 9 && AGE < 19) {
        System.out.println("Classification: New Born");
    } else if (AGE > 17 && AGE < 37) {
        System.out.println("Classification: TODDLER");
    } else if (AGE > 36 && AGE < 120) {
        System.out.println("Classification: KID");
    } else {
        System.out.println("Classification: Out of Range");
    }
}

附带说明,Java 约定是以大写字母开头的类名(child -> Child)。

【讨论】:

  • 哦,谢谢,所以我可以结合循环和条件构造。非常感谢
【解决方案2】:
public class child {
    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        int AGE;
        while(true){
            System.out.println("Enter Child's number(s) of Days: ");
            try {
                AGE = scan.nextInt(); 
            } catch (Exception e) {
                System.out.println ("Please enter a number!");
            }            

            if (AGE == 0 || AGE == 1){
                System.out.println("Classification: New Born");
            }
            else if (AGE >= 2 && AGE <= 10){
                System.out.println("Classification: Infant");
            }
            else if (AGE > 9 && AGE < 19){
                System.out.println("Classification: New Born");
            }
            else if (AGE > 17 && AGE < 37){
                System.out.println("Classification: TODDLER");
            }
            else if (AGE > 36 && AGE < 120 ) {
                System.out.println("Classification: KID");
            }
            else{
                System.out.println("Classification: Out of Range");
            }
        }
    }
}

希望这会有所帮助。

【讨论】:

    猜你喜欢
    • 2017-09-11
    • 2022-01-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-04-02
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多