2015-11-07 124 views
0

我有這個計算器代碼,它使用用戶後綴表達式和一個文件輸入取決於用戶的偏好,我需要創建一個循環,如果任何後綴表達式不正確,(3 3 +,3 x 3,34 43 43等)它已經給用戶一個關於錯誤的正確的錯誤信息,但我需要它再次啓動文件輸入和鍵盤輸入循環,從開頭用戶在k或f中輸入他們想要的輸入。循環計算器代碼

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

public class Test { 
public static void main(String[] args) throws FileNotFoundException { 
    Scanner Scanner = new Scanner(System.in); 
    System.out.println("Press K to enter the input using the keyboard. Press F to read expressions from a file"); 
    String option = Scanner.nextLine(); 
    while (!(option.equalsIgnoreCase("K") || option.equalsIgnoreCase("F"))) { 
     System.out.println("Invalid option - please choose again"); 
     option = Scanner.nextLine(); 
    } 
    if (option.equalsIgnoreCase("K")) { 

     Scanner Scanner2 = new Scanner(System.in); 

     System.out.println("Please enter a postfix expression: "); 

     String line = Scanner2.nextLine(); 

     String[] elements = line.split(" "); 

     if (elements.length != 3) { 
      System.out.println("Error: Invalid Expression: " + line); 

     } 

     else { 
      try { 
       // "try" is a special statement which allows us to deal with 
       // "exceptions" 

       double num1 = Double.parseDouble(elements[0]); 
       double num2 = Double.parseDouble(elements[1]); 

       double answer = 0; 
       boolean OpperationWrong = false; 
       String operation = elements[2]; 
       switch (operation) { 

       case "+": { 
        answer = (num1 + num2); 
        System.out.println("Answer is: = " + answer); 
       } 
        break; 

       case "-": { 
        answer = (num1 - num2); 
        System.out.println("Answer is: = " + answer); 

       } 
        break; 

       case "/": { 
        answer = (num1/num2); 
        System.out.println("Answer is: = " + answer); 

       } 
        break; 

       case "*": { 
        answer = (num1 * num2); 
        System.out.println("Answer is: = " + answer); 
       } 
        break; 

       default: 
        OpperationWrong = true; 
       } 
       String ans; 
       if (OpperationWrong) { 
        System.out.println("Please use +,*,/,- for your operator"); 
       } else { 
        ans = String.valueOf(answer); 
       } 
      } catch (NumberFormatException e) { 
       System.out.println("Invalid Key entered, please enter a number for first 2 elements"); 
      } 
     } 
    } else if (option.equalsIgnoreCase("F")) { 

     try { 
      Scanner Scanner3 = new Scanner(System.in); 
      System.out.print("Enter the file name to read expressions from : "); 
      File file = new File(Scanner3.nextLine()); 
      Scanner3 = new Scanner(file); 

      while (!(file.exists())) { 
       System.out.println("Error: That file does not exist, please re-enter: "); 

      } 

      String line; 

      while (Scanner3.hasNext()) { 
       line = Scanner3.nextLine(); // read the next line of text 
              // from the file 
       String[] elements = line.split(" "); 
       if (elements.length != 3) { 
        System.out.println("Error: Invalid Expression: " + line); 
       } else { 
        try { 
         // "try" is a statement which allows us to deal with 
         // "exceptions" 

         double num1 = Double.parseDouble(elements[0]); 
         double num2 = Double.parseDouble(elements[1]); 

         double answer = 0; 
         boolean OpperationWrong = false; 
         String operation = elements[2]; 
         switch (operation) { 

         case "+": { 
          answer = (num1 + num2); 
          System.out.println("Answer is: = " + answer); 
         } 
          break; 

         case "-": { 
          answer = (num1 - num2); 
          System.out.println("Answer is: = " + answer); 

         } 
          break; 

         case "/": { 
          answer = (num1/num2); 
          System.out.println("Answer is: = " + answer); 

         } 
          break; 

         case "*": { 
          answer = (num1 * num2); 
          System.out.println("Answer is: = " + answer); 
         } 
          break; 

         default: 
          OpperationWrong = true; 
         } 
         String ans; 
         if (OpperationWrong) { 
          ans = " Please use +,*,/,- for your operator"; 
         } else { 
          ans = String.valueOf(answer); 
         } 
        } catch (NumberFormatException e) { 
         System.out.println("Invalid expression please enter a number for first 2 elements"); 
        } 
       } 
      } 
     } catch (FileNotFoundException exception) { 
      System.out.println("The file could not be found"); 
     } 
    } 
} 
} 
+0

檢查這題。 http://stackoverflow.com/questions/33585160/create-a-java-calculator/33585372#33585372 – panagdu

回答

0

的幾件事情

它看起來像你可以在一個非常強大的方式來解決你的問題在這裏利用的功能。我也注意到你可能想要仔細觀察你如何使用和命名你的變量。

功能

函數或方法是任何編程語言,當你可以做東西和發現自己想要做的東西再次,這是你如何識別你想要的一個極其重要的組成部分功能或方法。

你已經完成了上面的難題,因爲你的代碼完成了你想要的任務。現在你想再做一遍,所以我們可以把這個操作看作是調用一個函數。

您可能需要添加退出出來的功能,如果你選擇來實現它在用戶按下「X」或類似的東西,我會離開,鍛鍊了你的能力。

變量

有,你初始化時,你已經可以訪問它的變量少數情況下,最明顯的對我來說是掃描儀。當您聲明並初始化Scanner Scanner = new Scanner(System.in)時,只要您有權訪問Scanner變量,就可以訪問System.in(另一個技巧是使用小寫字母來命名變量,其中Scanner和Scanner稍後會不明確,如果你指的是變量或類)。後來做Scanner Scanner2 = new Scanner(System.in)是不必要的。

還有一些其他的清理工作可以用變量來完成,但是我會把它們留給你。

代碼
import java.io.File; 
import java.io.FileNotFoundException; 
import java.util.Scanner; 

public class Test { 
    public static void main(String[] args) throws FileNotFoundException { 
     Scanner scanner = new Scanner(System.in); 
     boolean quitting = false; 
     while (!quitting){ 
      quitting = processInput(scanner); 
     } 
    } 

    public static boolean processInput(Scanner scanner){ 
     System.out.println("Press K to enter the input using the keyboard. Press F to read expressions from a file"); 
     String option = scanner.nextLine(); 
     while (!(option.equalsIgnoreCase("K") || option.equalsIgnoreCase("F"))) { 
      System.out.println("Invalid option - please choose again"); 
      option = scanner.nextLine(); 
     } 
     if (option.equalsIgnoreCase("K")) { 
      System.out.println("Please enter a postfix expression: "); 

      String line = scanner.nextLine(); 

      String[] elements = line.split(" "); 

      if (elements.length != 3) { 
       System.out.println("Error: Invalid Expression: " + line); 

      } 
      else { 
       try { 
        // "try" is a special statement which allows us to deal with 
        // "exceptions" 

        double num1 = Double.parseDouble(elements[0]); 
        double num2 = Double.parseDouble(elements[1]); 

        double answer = 0; 
        boolean OpperationWrong = false; 
        String operation = elements[2]; 
        switch (operation) { 

        case "+": { 
         answer = (num1 + num2); 
         System.out.println("Answer is: = " + answer); 
        } 
         break; 

        case "-": { 
         answer = (num1 - num2); 
         System.out.println("Answer is: = " + answer); 

        } 
         break; 

        case "/": { 
         answer = (num1/num2); 
         System.out.println("Answer is: = " + answer); 

        } 
         break; 

        case "*": { 
         answer = (num1 * num2); 
         System.out.println("Answer is: = " + answer); 
        } 
         break; 

        default: 
         OpperationWrong = true; 
        } 
        String ans; 
        if (OpperationWrong) { 
         System.out.println("Please use +,*,/,- for your operator"); 
        } 
        else { 
         ans = String.valueOf(answer); 
        } 
       } catch (NumberFormatException e) { 
        System.out.println("Invalid Key entered, please enter a number for first 2 elements"); 
       } 
      } 
     } 
     else if (option.equalsIgnoreCase("F")) { 
      try { 
       System.out.print("Enter the file name to read expressions from : "); 
       File file = new File(scanner.nextLine()); 
       Scanner fileScanner = new Scanner(file); 

       while (!(file.exists())) { 
        System.out.println("Error: That file does not exist, please re-enter: "); 

       } 

       String line; 

       while (fileScanner.hasNext()) { 
        line = fileScanner.nextLine(); // read the next line of text 
               // from the file 
        String[] elements = line.split(" "); 
        if (elements.length != 3) { 
         System.out.println("Error: Invalid Expression: " + line); 
        } 
        else { 
         try { 
          // "try" is a statement which allows us to deal with 
          // "exceptions" 

          double num1 = Double.parseDouble(elements[0]); 
          double num2 = Double.parseDouble(elements[1]); 

          double answer = 0; 
          boolean OpperationWrong = false; 
          String operation = elements[2]; 
          switch (operation) { 

          case "+": { 
           answer = (num1 + num2); 
           System.out.println("Answer is: = " + answer); 
          } 
           break; 

          case "-": { 
           answer = (num1 - num2); 
           System.out.println("Answer is: = " + answer); 

          } 
           break; 

          case "/": { 
           answer = (num1/num2); 
           System.out.println("Answer is: = " + answer); 

          } 
           break; 

          case "*": { 
           answer = (num1 * num2); 
           System.out.println("Answer is: = " + answer); 
          } 
           break; 

          default: 
           OpperationWrong = true; 
          } 
          String ans; 
          if (OpperationWrong) { 
           ans = " Please use +,*,/,- for your operator"; 
          } 
          else { 
           ans = String.valueOf(answer); 
          } 
         } catch (NumberFormatException e) { 
          System.out.println("Invalid expression please enter a number for first 2 elements"); 
         } 
        } 
       } 
      } catch (FileNotFoundException exception) { 
       System.out.println("The file could not be found"); 
      } 
     } 
     return false; 
    } 
} 
0

嘗試具有同時(true)循環,並打破如果輸入是可接受的。

一個簡單的例子:

int input=0; 
while(true){ 
input =new Scanner(System.in).nextInt(); 
if(input<=0){ 
System.out.println("Illegal input!"); 
continue; 
} 
break; 
} 
0

NB: - 你應該在JAVA使用小寫字母爲變量的名稱等Scanner scanner。 - 您應爲每個來源使用一個掃描儀,例如System.innew File("name of file")。 - 對於鍵盤掃描儀(System.in),建議使用static掃描儀變量,如:private static Scanner scanner = new Scanner(System.in);。 如果你想從頭循環當您使用掃描儀與路徑文件Scanner(File source)

- 你應該關閉您的掃描儀,你可以嘗試使用一個無限循環while(true)這樣的:

public class Test { 
// lower case of name variable 
private static Scanner scanner = new Scanner(System.in); 
public static void main(String[] args) throws FileNotFoundException { 
while(true){ 
System.out.println("Press K to enter the input using the keyboard. Press F to read expressions from a file"); 
String option = scanner.nextLine(); 
while (!(option.equalsIgnoreCase("K") || option.equalsIgnoreCase("F"))) { 
    System.out.println("Invalid option - please choose again"); 
    option = scanner.nextLine(); 
} 
if (option.equalsIgnoreCase("K")) { 

    System.out.println("Please enter a postfix expression: "); 

    String line = scanner.nextLine(); 

    String[] elements = line.split(" "); 

    if (elements.length != 3) { 
     System.out.println("Error: Invalid Expression: " + line); 

    } 

    else { 
     try { 
      // "try" is a special statement which allows us to deal with 
      // "exceptions" 

      double num1 = Double.parseDouble(elements[0]); 
      double num2 = Double.parseDouble(elements[1]); 

      double answer = 0; 
      boolean OpperationWrong = false; 
      String operation = elements[2]; 
      switch (operation) { 

      case "+": { 
       answer = (num1 + num2); 
       System.out.println("Answer is: = " + answer); 
      } 
       break; 

      case "-": { 
       answer = (num1 - num2); 
       System.out.println("Answer is: = " + answer); 

      } 
       break; 

      case "/": { 
       answer = (num1/num2); 
       System.out.println("Answer is: = " + answer); 

      } 
       break; 

      case "*": { 
       answer = (num1 * num2); 
       System.out.println("Answer is: = " + answer); 
      } 
       break; 

      default: 
       OpperationWrong = true; 
      } 
      String ans; 
      if (OpperationWrong) { 
       System.out.println("Please use +,*,/,- for your operator"); 
      } else { 
       ans = String.valueOf(answer); 
      } 
     } catch (NumberFormatException e) { 
      System.out.println("Invalid Key entered, please enter a number for first 2 elements"); 
     } 
    } 
} else if (option.equalsIgnoreCase("F")) { 

    try { 
     System.out.print("Enter the file name to read expressions from : "); 
     File file = new File(scanner.nextLine()); 
     Scanner scanner1 = new Scanner(file); 

     while (!(file.exists())) { 
      System.out.println("Error: That file does not exist, please re-enter: "); 

     } 

     String line; 

     while (scanner1.hasNext()) { 
      line = scanner1.nextLine(); // read the next line of text 
             // from the file 
      String[] elements = line.split(" "); 
      if (elements.length != 3) { 
       System.out.println("Error: Invalid Expression: " + line); 
      } else { 
       try { 
        // "try" is a statement which allows us to deal with 
        // "exceptions" 

        double num1 = Double.parseDouble(elements[0]); 
        double num2 = Double.parseDouble(elements[1]); 

        double answer = 0; 
        boolean OpperationWrong = false; 
        String operation = elements[2]; 
        switch (operation) { 

        case "+": { 
         answer = (num1 + num2); 
         System.out.println("Answer is: = " + answer); 
        } 
         break; 

        case "-": { 
         answer = (num1 - num2); 
         System.out.println("Answer is: = " + answer); 

        } 
         break; 

        case "/": { 
         answer = (num1/num2); 
         System.out.println("Answer is: = " + answer); 

        } 
         break; 

        case "*": { 
         answer = (num1 * num2); 
         System.out.println("Answer is: = " + answer); 
        } 
         break; 

        default: 
         OpperationWrong = true; 
        } 
        String ans; 
        if (OpperationWrong) { 
         ans = " Please use +,*,/,- for your operator"; 
        } else { 
         ans = String.valueOf(answer); 
        } 
       } catch (NumberFormatException e) { 
        System.out.println("Invalid expression please enter a number for first 2 elements"); 
       } 
      } 
     } 
//   close your Scanner 
    scanner1.close(); 

    } catch (FileNotFoundException exception) { 
     System.out.println("The file could not be found"); 
    } 
} 
} 
} 
}