Do语句终止后如何获得两个结果?

时间:2014-09-29 18:43:41

标签: java arrays string

在Do Else While语句之后我需要两个结果,现在用户可以输入数据并且它将存储在String中,如果他们想要添加任何他们键入'y'的内容,如果'n'它将结束该计划并告诉他们他们已经输入了什么。如果他们没有输入那些并且输入'd'例如它会停止运行语句并将我带到Else语句

在Else声明中,我想要两个结果,或者“你添加了以下内容”和“错误,你输错了”

以下是Do Else While声明:

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'或写错了什么)。

5 个答案:

答案 0 :(得分: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);

答案 1 :(得分: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);

答案 2 :(得分:0)

尝试使用" IF"其他陈述中的陈述

    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;
        }

    }

答案 3 :(得分: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);

答案 4 :(得分: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循环更容易让其他人理解他们可以在进入循环体之前评估条件。

您也不需要布尔值,只需在while循环中使用true,在break;部分使用else if,但与while循环一样,中断可以一旦你开始编写更大的程序,其他人需要查看你的代码时会产生混淆。