我如何做一个do-while循环?

时间:2015-05-10 03:18:55

标签: java do-while

我正在努力提高我的do-while循环技能,而且我很难尝试实现一个do-while循环,每次循环都没有找到循环。

   public static void getinput() throws IOException {

    Scanner scanner = new Scanner(System.in);
    String input = null;
    /* End Initialization */

    System.out.println("Welcome ");
    System.out.println("What would you like to know?");
  do{
    System.out.print("> ");
    input = scanner.nextLine().toLowerCase();
    parseFile(input);

    System.out.println("is there anything you want to know?");
    }
  while(input.contains("bye")
 {
 System.out.println("have a good day");
  }

有人可以帮我解决我的问题吗?

4 个答案:

答案 0 :(得分:0)

语法如下:

$?

然而,请注意,只要用户说"再见"这将继续循环。 (也许你想要do{ System.out.print("> "); input = scanner.nextLine().toLowerCase(); parseFile(input); System.out.println("is there anything you want to know?"); } while(input.contains("bye"); // Semicolon after the "while." This is the end of the statement // No brackets here. System.out.println("have a good day");

答案 1 :(得分:0)

该行

while(input.contains("bye")

缺少分号,应该测试相反的情况,并且该行之后的代码不需要括在括号中。否则你的代码看起来很好。

答案 2 :(得分:0)

我相信你正在寻找类似的东西,

Scanner scanner = new Scanner(System.in);
String input = "";
System.out.println("Welcome ");
System.out.println("What would you like to know?");
do {
    System.out.print("> ");
    input = scanner.nextLine().toLowerCase();
    parseFile(input);

    System.out.println("is there anything you want to know?");
} while (!input.startsWith("bye")); // <-- while it doesn't...
System.out.println("have a good day");

答案 3 :(得分:0)

做语法 做{      声明(S) } while(表达式); 你的守则      public static void getinput()throws IOException {         扫描仪扫描仪=新扫描仪(System.in);         String input = null;         / *结束初始化* /

    System.out.println("Welcome ");
    System.out.println("What would you like to know?");
  do{
    System.out.print("> ");
    input = scanner.nextLine().toLowerCase();
    parseFile(input);
    System.out.println("is there anything you want to know?");
    }
  while(input.contains("bye"));//right parenthesis add semicolon 
                         OR 
  while(input.equalsIgnoreCase("bye"));
//remove braces from here.
 System.out.println("have a good day");