2014-09-29 48 views
-1

我在Do Else While语句后需要两个结果,现在用户可以输入数据,并且它将存储在一个字符串中,如果他们想要添加其他任何他们键入的'y',如果'n'它会结束编程并告诉他们他们输入了什么。如果他们输入既不是那些和输入“d”的,例如它停止语句运行,带我一直到else语句如何在Do语句终止后得到两个结果?

else语句我想两个结果,无论是“你已经添加了以下”和“错误,你输入什么不对”

这里是DO否则虽然声明:

do { 
     System.out.println("Current list is " + list); 
     System.out.println("Add more? (y/n)"); 
     if (input.next().startsWith("y")) { 
      System.out.println("Enter : "); 
      list.add(input.next()); 
     } else { 
      System.out.println("You have added the following:"); 
      System.out.println("Error, you inputted something wrong"); 
      break; 

     } 
    } while (true); 

什么我写来获得这取决于用户做了两个结果? (说'n'或写错了什么)。

+0

只是放了另一个'if'(或更好的'else if')。 – qqilihq 2014-09-29 18:45:33

+0

你会认为“错误”是什么?一个数字而不是一个字符串? – MarGar 2014-09-29 18:47:13

+0

第一次调用'input.next()'读取一个单词,第二次调用读取第二个单词,所以你会读'y 1 y 2 y 3 y 4 y 5',但是你不能说'n '因为这将是一个错误。 :P – 2014-09-29 18:47:25

回答

0

试试这个:

do { 
    System.out.println("Current list is " + list); 
    System.out.println("Add more? (y/n)"); 
    if (input.next().startsWith("y")) { 
     System.out.println("Enter : "); 
     list.add(input.next()); 
    } else if (input.next().startsWith("n")) { 
     System.out.println("You have added the following:"); 
     break; 
    } else { 
     System.out.println("Error, you inputted something wrong"); 
    } 
} while (true); 
+0

谢谢,这完美! – AAP 2014-09-29 19:36:48

0

在else中引入另一个条件。最好使用其他如:

do { 
    System.out.println("Current list is " + list); 
    System.out.println("Add more? (y/n)"); 
    String userInput = input.next(); 
    if (userInput.startsWith("y")) { 
     System.out.println("Enter : "); 
     list.add(input.next()); 
    } elseif (userInput.startsWith("n")) { 
     // user wants to stop 
     System.out.println("You have added the following:"); 
     break; 
    } else { 
     System.out.println("Error, you inputted something wrong"); 
     break; 
    } 
} while (true); 
+0

谢谢,这工作!我现在唯一的问题是它在注册之前需要两个输入。这是输出: 添加更多? ñ ň 您已经添加以下(Y/N): – AAP 2014-09-29 19:33:41

1

只需要添加另外if-else:

do { 
    System.out.println("Current list is " + list); 
    System.out.println("Add more? (y/n)"); 
    if (input.next().startsWith("y")) 
    { 
    System.out.println("Enter : "); 
    list.add(input.next()); 
    } 
    else 
    { 
    if(//valid input condition) 
     System.out.println("You have added the following:"); 
     else 
     System.out.println("Error, you inputted something wrong"); 
    break; 
    } 
} while (true); 
0

尝试使用 “IF” 语句中else语句

if (input.next().startsWith("y")) { 
     System.out.println("Enter : "); 
     list.add(input.next()); 
    } else { 
     if (input.next().startsWith("n")) { 
      // Your code for "n" 
     }else{ 
      //else here. 
      System.out.println("You have added the following:"); 
      System.out.println("Error, you inputted something wrong"); 
      break; 
     } 

    } 
0

我认为你的程序的一些逻辑和流程稍微偏离。我会改变这一点。

代码:

boolean keepGoing = true; // can use a boolean to change the while loop condition to false. 

    while (keepGoing) { 

     System.out.println("Enter : "); 
     list.add(input.next());    

     System.out.println("Current list is " + list); 
     System.out.println("Add more? (y/n)"); 

     if (input.next().startsWith("y")) { // 'if' to check if 'y', then execute this code.   

      keepGoing = true; // don't really need this, but it's here as example 

     } else if (input.next().startsWith("n")){ // 'else if' to check if 'n'. 

      System.out.println("You have added the following: " + list);    
      keepGoing = false; //change to false to stop the loop 

     } else { // and lastly a single 'else' if the input was invalid based on 2 previous conditions. 

     System.out.println("Error, you inputted something wrong"); // if for some reason the input isn't accepted this will show. 

     } 

    } 

它遵循一个更合乎逻辑的流动,更容易理解。一个简单的while循环对其他人来说更容易理解,他们可以在进入循环体之前评估条件。

你也不需要布尔值,可以简单地在else if部分while循环和break;使用true,但与while循环,休息可以尽可能在别人需要看你的代码中创建的困惑一旦你开始写更大的程序。