如何退出while循环?

时间:2016-10-23 01:43:17

标签: java

我有一个包含四种不同方法的代码(下图没有图片)并创建了一个菜单供用户选择他们想要选择的方法。

除非用户输入5,否则如何将方法循环到菜单屏幕的位置?

import java.util.Scanner;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.PrintWriter;

public class ReactionAnalysis {
  public static void main (String [] args) throws FileNotFoundException
  {
    /// Menu Screen
    System.out.println("What would you like to do?");
    System.out.println("1: Get the score of a word");
    System.out.println("2: Get the average score of words in a file (one word per line)");
    System.out.println("3: Find the highest/lowest scoring words in a file");
    System.out.println("4: Sort words from a file into positive.txt and negative.txt");
    System.out.println("5: Exit the program");

    Scanner in = new Scanner(System.in);
    System.out.println("Enter a number 1-5");
    int numberChoice = in.nextInt();
    ////

    while (numberChoice != 5)
    {
      if (numberChoice == 1)
      {
        String methodOne = methodOne();
        System.out.println(methodOne);
      }
      else if (numberChoice == 2)
      {
        String methodTwo = methodTwo();
        System.out.println(methodTwo);
      }
      else if (numberChoice == 3)
      {
        String methodThree = methodThree();
        System.out.println(methodThree);
      }
      else if (numberChoice == 4)
      {
        String methodFour = methodFour();
        System.out.println(methodFour);
      }
    }
}
}

现在,在我完全完成其中一个步骤之后,它只是重复了这一步骤。我需要它来完成步骤,然后返回菜单,并重复,除非在菜单中输入5。

1 个答案:

答案 0 :(得分:0)

很抱歉格式化,只需复制粘贴大部分代码:我将循环移动到顶部,并将int初始化为0,因此它至少会运行一次。

    import java.util.Scanner;
    import java.io.File;
    import java.io.FileNotFoundException;
    import java.io.PrintWriter;

    public class ReactionAnalysis {
      public static void main (String [] args) throws FileNotFoundException
      {
        int numberChoice = 0;
    //loop everything
    while (numberChoice != 5)
    {
        /// Menu Screen
    System.out.println("What would you like to do?");
    System.out.println("1: Get the score of a word");
    System.out.println("2: Get the average score of words in a file (one word per line)");
    System.out.println("3: Find the highest/lowest scoring words in a file");
    System.out.println("4: Sort words from a file into positive.txt and negative.txt");
    System.out.println("5: Exit the program");

    Scanner in = new Scanner(System.in);
    System.out.println("Enter a number 1-5");
    numberChoice = in.nextInt();
    ////

      if (numberChoice == 1)
      {
        String methodOne = methodOne();
        System.out.println(methodOne);
      }
      else if (numberChoice == 2)
      {
        String methodTwo = methodTwo();
        System.out.println(methodTwo);
      }
      else if (numberChoice == 3)
      {
        String methodThree = methodThree();
        System.out.println(methodThree);
      }
      else if (numberChoice == 4)
      {
        String methodFour = methodFour();
        System.out.println(methodFour);
      }
    }
}
}