如何在每次无效输入后要求用户反复输入 [英] how to ask user to input again and again after every invalid input

查看:157
本文介绍了如何在每次无效输入后要求用户反复输入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我自己的有效日期只有1520到3999,我是这个网站的新手,我是新的java
请帮助
i想知道我做错了什么或在这段代码中做了什么。

so my own valid dates only are from 1520 to 3999 and im new to this site and im new to java please help i want to know what did i do wrong or what to do in this code.

    int x;
    Scanner in = new Scanner (System.in);
    do
    {
        System.out.print ("Enter a date ");
        x = in.nextInt();
    }
    while (x<1520&&x>3999);
    {
        System.out.println ("Invalid Gregorian Calendar date.");
        System.out.print ("Please Input a valid Gregorian Calendar date: ");
    }
}}


推荐答案

在while循环中,只要输入无效,就获取另一个整数。此外,您需要在while条件下将&& 更改为 || ,因为没有数字都少于您还不需要执行部分,并且需要在while循环后删除分号 - 它阻止循环执行任何有效的操作对你而言。

In the while loop, get another integer whenever the input was invalid. Also, you need to change && to || in your while condition, as no number is both less than 1520 and greater than 3999. You also do not need the do section, and you need to remove the semicolon after your while loop - it was preventing the loop from doing anything productive for you.

int x;
Scanner in = new Scanner (System.in);
System.out.print ("Enter a date ");
x = in.nextInt();

while (x<1520 || x>3999) // change this to "or", i.e. && --> || 
    {
        System.out.println ("Invalid Gregorian Calendar date.");
        System.out.print ("Please Input a valid Gregorian Calendar date: ");
        x = in.nextInt(); // get another number before checking conditions again
    }

这篇关于如何在每次无效输入后要求用户反复输入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆