Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/374.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 使用do while循环进行提示,can';Don’我似乎没有参与_Java - Fatal编程技术网

Java 使用do while循环进行提示,can';Don’我似乎没有参与

Java 使用do while循环进行提示,can';Don’我似乎没有参与,java,Java,我正在努力使代码的“do/while循环”部分正常工作。我必须使用它,因为它是作业的一部分。我的目标是在无效输入时引导用户返回输入。日期的有效输入格式为mm/dd/yyyy。因此,如果用户输入229214,我想向他显示错误消息并提示他重新输入日期 我对java编码非常陌生,但我知道我离这个问题的正确解决方案不远。。请尽你所能帮助我 String date; //user entered date int slash1; //position of the first slash int slas

我正在努力使代码的“do/while循环”部分正常工作。我必须使用它,因为它是作业的一部分。我的目标是在无效输入时引导用户返回输入。日期的有效输入格式为mm/dd/yyyy。因此,如果用户输入229214,我想向他显示错误消息并提示他重新输入日期

我对java编码非常陌生,但我知道我离这个问题的正确解决方案不远。。请尽你所能帮助我

String date; //user entered date
int slash1; //position of the first slash
int slash2; //position of the second slash
boolean isValid; // true if date is valid
String day; // day part of the input date becomes a separate string
String month; // month part of the input date becomes a separate string
String year; // year part of the input date becomes a separate string
isValid = true; //initializing to true
do
{
  System.out.print("Enter a date (mm/dd/yyyy on or after 1860, such as 3/10/2015): ");
  date = stdIn.next(); //storing the input date
  slash1 = date.indexOf('/'); //position of the first slash in the user string
  slash2 = date.indexOf('/', slash1+1); //it looks for the position of the second string after the first one
  if (slash1 >= 0 && slash2 >= 0 && slash2 > (slash1+1))
  {
    month = date.substring(0, slash1);
    day = date.substring(slash1+1, slash2);
    year = date.substring(slash2+1);
    for (int i = 0; i < month.length(); i++)
    {
      if (!(Character.isDigit(month.charAt(i))))
      isValid = false;
    }
    for (int i = 0; i < day.length(); i++)
    {
      if (!(Character.isDigit(day.charAt(i))))
      isValid = false;
    }
    for (int i = 0; i < year.length(); i++)
    {
      if (!(Character.isDigit(year.charAt(i))))
      isValid = false;
    }
  } System.out.println("Invalid date: " + date + ". Please re-enter.");
} while (isValid == false);
字符串日期//用户输入的日期
int-slash1//第一条斜线的位置
int-slash2//第二条斜线的位置
布尔值是有效的;//如果日期有效,则为true
字符串日;//输入日期的日期部分成为单独的字符串
字符串月份;//输入日期的月份部分成为单独的字符串
字符串年份;//输入日期的年份部分成为单独的字符串
isValid=true//初始化为true
做
{
系统输出打印(“输入日期(1860年当日或之后,如2015年3月10日):”;
date=stdIn.next();//存储输入日期
slash1=date.indexOf('/');//用户字符串中第一个斜杠的位置
slash2=date.indexOf('/',slash1+1);//它查找第一个字符串之后的第二个字符串的位置
如果(slash1>=0&&slash2>=0&&slash2>(slash1+1))
{
月=日期。子字符串(0,slash1);
day=日期子字符串(slash1+1,slash2);
年份=日期。子字符串(slash2+1);
对于(int i=0;i
while循环的条件为

isValid=false
我想你是说

isValid==false

我认为至少有一个与do{}while()循环无关的问题是,在循环中从未将isValid变量设置为true。它一开始是真的,但是如果你输入了一个无效的日期,程序会再次提问,但它不会回到假设它是真的。CyanogenCX也是正确的,因为您需要双等号来测试相等性,而不是单等号来进行赋值。请尝试更改以下行:

isValid=true;

从do while循环中“do”的前一行到它的后一行。

如果(slash1>=0&&slash2>=0&&slash2>(slash1+1)),是否在调试代码时将断点设置为:
?以及单步完成(例如,Eclipse中的[F6])

isValid
永远不会变为
false
,无论您输入的日期是有效日期还是无效日期,只要它包含数字:
1/1/1
99/99/99。

isValid
仅当您输入字母:
a/1/1
时才会变为
false

此外,每次都会显示错误消息,因为它不依赖于
isValid


还有一些地方需要改进。我可以建议你去旅游吗?

谢谢你的关注!但它仍然没有回到循环中。。我能想到的唯一其他原因是,for循环中if语句的任何条件都不满足。因此isValid从不设为false我可能会建议调试,看看它是否有效:if(!(Character.isDigit(day.charAt(i))==true)---或者试试这个,看看它是否继续循环——if(!(Character.isDigit(day.charAt(i))==false)提示:
mm/dd/yyy
3/10/2015
矛盾。根据你的格式,后者应该是
03/10/2015
。是的,我知道,但在作业的这一部分,这种格式(3/10/2015)可以输入,并且应该生成有效的消息,只要它有两个斜杠和数字。我试着将代码行移到“do”之后的行,但它仍然没有循环。即使你有更多的bug,你也需要它;尝试更新包含问题的代码,否则其他人会看到相同的问题。我不知道如何调试或使用F6单步完成。。我的代码为输入的相应格式提供假消息和真消息-2016年3月、2016年3月和2016年3月为假消息,2014年2月200日为真消息,这是我在本阶段的要求-验证至少2个斜杠,无空字符串,日期仅包含数字。在下一阶段,我唯一需要更改的是,根据上一阶段的条件,如果日期为假,请用户重新输入日期。我似乎无法让它发挥作用。