while循环Break最有效的方法

时间:2018-04-23 00:50:01

标签: java while-loop

当我设置Case C时,它正确地遍历列表,但在我输入字符串之前它不会返回主菜单。我想让它像其他两个一样回归,有什么想法吗?它不会卡在循环中,我不明白为什么输入一个字符串返回菜单。所有案例逻辑都能正常工作。

public class Postfix_Notation
{
static ArrayList<String> wordlist = new ArrayList<>(); 

public static void main(String[] args) 
{
 Scanner scanner = new Scanner(System.in);
 String choice = "Z";


     while(!choice.equals("D"))
  {
    System.out.println("Enter A, B  or C to perform an operation or enter D to exit.");
    System.out.println("A- Evaluate a user input postfix expressions");
    System.out.println("B- Convert, display infix expressions to postfix expressions then evaluate and display the result of thepostfix expression");
    System.out.println("C- Reads words from a the text file (hangman.txt) in a LinkedList and use an iterator on it to displays all the words (duplicates allowed) in descending alphabetical order");
    System.out.println("D- Exit");
    choice = new Scanner(System.in).next();

        switch (choice) 
         {
          case "A":
          Scanner input3 = new Scanner(System.in);
          System.out.println("Enter an postfix expression, do not add spaces! I.E. 12-3*3");
          String postfix = new Scanner(System.in).next();
          System.out.println(evaluatePostfix(postfix));
          System.out.println("\n");
          break;

          case "B":
          Scanner input2 = new Scanner(System.in);
          System.out.println("Enter an inflix expression, do not add spaces! I.E. 1*2(3+9)");
          String inflix = new Scanner(System.in).next();
      System.out.println("The posfix expression of " + inflix + "is " + infixToPostfix(inflix));
          System.out.println("\n");    
          break;

          case "C":
          Scanner input = new Scanner(System.in);
          String content = new String();
          int count=1;
          int n;
          File file = new File("Hangman.txt");
          LinkedList<String> list = new LinkedList<String>();
          //file.length();

          try 
            {
              Scanner sc = new Scanner(new FileInputStream(file));
              while (sc.hasNextLine())
              {
                   content = sc.nextLine();
                   list.add(content);
              }
               sc.close();
              }
              catch(FileNotFoundException fnf)
              {
               fnf.printStackTrace();
              }
               catch (Exception e) 
              {
               e.printStackTrace();
               System.out.println("\nProgram terminated Safely...");
              }

               Collections.reverse(list);
               Iterator i = list.iterator();
               while (i.hasNext()) 
               {
                System.out.println(i.next());
               }
               File hangman = new File("hangman.txt"); //hangman.txt must be in the project folder or the program terminates
               if (!hangman.exists()) 
              {
                System.out.println(hangman.getAbsolutePath());
                System.out.println(hangman + "File does not exist.");
                System.exit(1);
              }

             try 
             {
               Scanner input4 = new Scanner(hangman);
               while (input.hasNext()) 
               {
                wordlist.add(input.next());
                break;
               }
              } 

             catch (FileNotFoundException ex) 
             {
              ex.printStackTrace();
             }
               System.out.println("Enter a Character to return to menu");
               System.out.println("\n");
               break;

               case "D":
               System.out.println("Program Ended");
               break;

               default:
               System.out.println("Invalid Input");  
     }  
  }

0 个答案:

没有答案