2013-10-17 88 views
1

我想知道为什么当我被问到在这个方法中是否有更多的数字时,输入'y'代码能够正常工作并且离开循环,但是当输入'n'并且返回时,我需要键入'n'并再次返回,否则该过程就会挂起。Java:输入比较

为什么?

字符串'input'从主方法的用户输入中传递,在此情况下为“addition”。

private int add(String input) 
    { 
     int additionValue = 0; 
     boolean keepGoing = true; 
     if (input.matches("addition")) 
     { 

      while (keepGoing == true) 
      { 
       System.out.println("Next digit = (Type the digit)"); 
       additionValue = additionValue + scan.nextInt(); 
       System.out.println("Any more digits? Type y/n"); 
       if (scan.next().matches("Y|y")) 
       { 
        keepGoing = true; 
       } 
       else if (scan.next().matches("N|n")) 
       { 
        keepGoing = false; 
       } 
       else 
       { 
        System.out.println("Great, you broke it."); 
        System.exit(1); 
       } 
      } 
     } 
    } 

我已经成功地获取代码使用

System.out.println("Any more digits? Type y/n"); 
String yayOrNay = scan.next(); 
if (yayOrNay.length()==1 && yayOrNay.charAt(0)=='y') 
       { 
        keepGoing = true; 
       } 

工作,但似乎有点太复杂,我为所有它在做什么。

+0

辉煌,所有答复,谢谢,我现在明白我在那里混错! – hamc17

回答

4

scan.next()从输入蒸汽中拉出一个新字符。

因此,当您检查'n'并执行scan.next().matches("Y|y")时,实际上它会跳过'n'进行下一次比较。

解决的办法是分配scan.next()到一个变量,你可以使用:

 while (keepGoing == true) 
     { 
      System.out.println("Next digit = (Type the digit)"); 
      additionValue = additionValue + scan.nextInt(); 
      System.out.println("Any more digits? Type y/n"); 
      String next = scan.next(); 
      if (next.matches("Y|y")) 
      { 
       keepGoing = true; 
      } 
      else if (next.matches("N|n")) 
      { 
       keepGoing = false; 
      } 
      else 
      { 
       System.out.println("Great, you broke it."); 
       System.exit(1); 
      } 
     } 
0
   if (scan.next().matches("Y|y")) 
      { 
       keepGoing = true; 
      } 
      else if (scan.next().matches("N|n")) 

你叫scan.next 两次

一日一次检查,如果它的Y,如果它不是您是否再次输入

因此

 somevar=scan.next() 
     if (somevar.matches("Y|y")) 
     { 
      keepGoing = true; 
     } 
     else if (somevar.matches("N|n")) 
     { 
      keepGoing = false; 
     } 
     else .. 
0

您的错误是调用scan.next()两次,它会请求两个输入。

private int add(String input) 
{ 
    int additionValue = 0; 
    boolean keepGoing = true; 
    if (input.matches("addition")) 
    { 

     while (keepGoing == true) 
     { 
      System.out.println("Next digit = (Type the digit)"); 
      additionValue = additionValue + scan.nextInt(); 
      System.out.println("Any more digits? Type y/n"); 
      String s = scan.next(); 
      if (s.matches("Y|y")) 
      { 
       keepGoing = true; 
      } 
      else if (s.matches("N|n")) 
      { 
       keepGoing = false; 
      } 
      else 
      { 
       System.out.println("Great, you broke it."); 
       System.exit(1); 
      } 
     } 
    } 
} 
1

这是因为您调用了scan.next()两次。 你必须调用它一次,把结果字符串放入一个变量中。

String input2 = scan.next(); 
if (input2.matches("Y|y")) 
{ 
    keepGoing = true; 
} 
else if (input2.matches("N|n")) 
{ 
    keepGoing = false; 
} 
1

您需要输入'n'两次,因为您在每个if条件中扫描输入。所以如果这个字母不是'y',你的程序将在下一个if语句中等待用户输入。

你可以简单地做:

String yayOrNay = scan.next(); 
if (yayOrNay.matches("Y|y")) 
{ 
    keepGoing = true; 
} 
else if (yayOrNay.matches("N|n")) 
{ 
    keepGoing = false; 
} 
... 
0

请试试这个,

Scanner scan = new Scanner(System.in); 
     int additionValue=0; 
     while (true) 
     { 
      System.out.println("Any more digits? Type y/n"); 
      if (scan.next().matches("Y|y")) 
      { 
       System.out.println("Next digit = (Type the digit)"); 
       additionValue = additionValue + scan.nextInt(); 
       //System.out.println("Any more digits? Type y/n"); 
      } 
      else 
      { 
       System.out.println("Thanks"); 
       break; 
      } 
     } 

我不知道这个逻辑是否会为你工作。 “如果你愿意,你可以处理‘N’也”

输出

Any more digits? Type y/n 
y 
Next digit = (Type the digit) 
5 
Any more digits? Type y/n 
n 
Thanks