【发布时间】:2016-11-23 10:27:08
【问题描述】:
晚上好,
我不知道为什么这个循环根本不起作用。它毁了我的整个申请。下面是代码:
System.out.println("Please tell me what to count till?");
do
{
try
{
newEndingValue= input.nextInt();
if(newEndingValue >= 0 || newEndingValue <= 0)
{
break; //breaks the loop
}
}
catch (InputMismatchException e)
{
System.out.println("My Apologies, but COMMAND NOT RECOGNIZED!" + "\nPlease tell me what to count till?");
input.next();
}
}
while(!input.hasNextInt());
v.setEndingValue(newEndingValue);
System.out.println("Please tell me what to count from?");
if(increasingOrDecreasing.equalsIgnoreCase("Increasing")|| increasingOrDecreasing.equals("++") || increasingOrDecreasing.equals("+"))
{
do
{
try
{
newInitialValue = input.nextInt();
if(newInitialValue < v.getEndingValue())
{
break;
}
else{
System.out.println("My Apologies, but starting point value must be smaller than ending point value!" + "\nPlease tell me what to count from?");
newInitialValue = (v.getEndingValue()+10);//overrides the value to something that forces the loop back
}
}
catch (InputMismatchException e)
{
System.out.println("My Apologies, but COMMAND NOT RECOGNIZED!" + "\nPlease tell me what to count from?");
input.next();
}
}
while(!input.hasNextInt() || newInitialValue > v.getEndingValue());
}
else
{
do
{
try
{
newInitialValue = input.nextInt();
if(newInitialValue > v.getEndingValue())
{
break; //breaks the loop
}
else{
System.out.println("My Apologies, but starting point value must be larger than ending point value!" + "\nPlease tell me what to count from?");
newInitialValue = (v.getEndingValue()-10);//overrides the value something that forces the loop back
}
}
catch (InputMismatchException e)
{
System.out.println("My Apologies, but COMMAND NOT RECOGNIZED!" + "\nPlease tell me what to count from?");
input.next(); //consumes the erroneously typed string value
newInitialValue = (v.getEndingValue()-10);
}
}
while(!input.hasNextInt() || newInitialValue < v.getEndingValue());
}
所以输出是输入一个no,然后是1000,如下:
请告诉我数到多少?
没有
我很抱歉,但命令未被识别!
请告诉我数到多少?
1000
请告诉我从什么开始计算?
抱歉,起点值必须小于终点值!
请告诉我从什么开始计算?
为什么它直接进入第二个书面 else 语句? 为什么它会跳过 newInitialValue 的用户输入? 请注意,如果在为 newEndingValue 输入字符串后将结束值块后的代码编辑到下面,然后正确输入一个数字,这会消除我的错误,但如果再次运行并且用户配合,则会生成另一个错误:
...
newInitialValue = input.nextInt(); //essentially gets skipped over by compiler only when previous catch statement is triggered
System.out.println("Please tell me what to count from?");
if(increasingOrDecreasing.equalsIgnoreCase("Increasing")|| increasingOrDecreasing.equals("++") || increasingOrDecreasing.equals("+"))
{...
此外,由于它打印出“但起点值必须小于终点值”,我们可以推断出它分别与 if(incre...) 循环以及 do 和 try 循环一起工作。但它跳过了 (newI... = input...) 和 if(newIntia...) 代码行。我知道这个原因,即使手动输入 newInitialValue = 2(在参数内)它仍然会进入这个 else 子句。
【问题讨论】:
-
你的代码没有任何意义,你能指出逻辑在什么时候落入
else块? -
设置断点,看看代码如何流动。
-
当我运行它而不需要任何 catch 命令时,它运行良好。循环时捕获的某些内容适用于该值,但接下来的代码块被搞砸了。
-
if(newEndingValue >= 0 || newEndingValue <= 0)很好 -
如果您可以让人们更容易地查看问题,您将获得更好的帮助。 F.ex.:确保您发布的所有代码都是必要的,如果有不相关的部分,请将它们删除。使您的缩进有意义,例如没有比其中的语句更缩进的左括号。明确说出您正在谈论的
else是哪一个。说一下对应if的条件所涉及的变量的值是多少。当您这样做时,您很有可能会找出问题所在。
标签: java loops try-catch java.util.scanner do-while