繁体   English   中英

Java 后缀计算器

[英]Java postfix calculator

import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
public class CalculatorFinal {

    public static void main(String[] args) {
        // TODO Auto-generated method stub

        float total = 0;
        int operator;
        float operand1, operand2;
        String WELCOME="Welcome to your Postfix Calculator\n====================\n";
        String[] StrArray;
        String  postfix  ="";


        System.out.print(WELCOME);



            System.out.println("Enter your postfix expression, OR to exit type stop:");
            StrArray = postfix.split(" ");

            for ( int i = 0; i < postfix.length();  i++) {





       // try {

             //Scanner myfile = new Scanner(new File ("postfix.txt"));
            // while (myfile.hasNext())
            // {
                // postfix = myfile.nextLine();
                // StrArray = postfix.split(" ");


                 if((postfix.length() > 3)  && (operand1 = floatvalueof(postfix[0]) && (operand2 = floatvalueof(postfix[1]) && (operator = floatvalueof(postfix[2])))))
                    {
                      try { // This deals with exceptions 

                            switch(operator){
                            case '+':
                            total = operand1 + operand2;
                            break;
                            }

                            switch(operator){
                            case '-':
                            total = operand1 + operand2;
                            break;
                            }

                            switch(operator){
                            case '/':
                            total = operand1 + operand2;
                            break;
                            }

                            switch(operator){
                            case '*':
                            total = operand1 + operand2;
                            break;
                            }


                      }
                        catch (NumberFormatException e) 
                            {
                            System.out.println("Error:- "+e.getMessage());
                            }








}}}}

我一直在尝试使这个后缀计算器工作,以便如果有人输入 7 8 + 计算器会将其格式化为 7 + 8 并给出答案,但我似乎无法这样做。 任何帮助,将不胜感激。

您的代码有一些问题(或可以改进):

  1. 变量名应该是驼峰式大小写以提高可读性
  2. 分配您的操作数和数字并不是真的可读
  3. 您根本不阅读用户的输入
  4. 您有 4 个不同的开关盒,每个开关盒处理一个案例。 开关的整个想法是将多个案例(和一个默认处理程序)放在 1 个开关中
  5. 你没有显示你的计算结果
  6. 当有人进入一个未知的接线员时,你什么都不做

我强烈建议您进行调试,以便您可以逐行查看代码在做什么。

这是您的代码的改进/修复版本,因此它可以正常工作并正确处理上述情况:

  public static void main(String[] args) throws IOException
  {
    Double result = 0D;

    System.out.print("Welcome to your Postfix Calculator\n====================\n");
    System.out.println("Enter your postfix expression, OR to exit type stop:");
    BufferedReader bufferRead = new BufferedReader(new InputStreamReader(System.in));
    String input = bufferRead.readLine();
    if (input.toLowerCase().equals("stop"))
    {
      System.out.println("Received stop command.");
      return;
    }

    String[] inputArray = input.split(" ");
    if (inputArray.length == 3)
    {
      try
      {
        Double number1 = Double.parseDouble(inputArray[0]);
        Double number2 = Double.parseDouble(inputArray[1]);
        String operator = inputArray[2];

        switch (operator)
        {
          case "+":
            result = number1 + number2;
            break;

          case "-":
            result = number1 - number2;
            break;

          case "/":
            result = number1 / number2;
            break;

          case "*":
            result = number1 * number2;
            break;

          default:
            System.out.println("Received unsupported operator: " + operator);
            break;
        }
      }
      catch (NumberFormatException e)
      {
        System.err.println("Error:\n" + e.getMessage());
      }
    }
    else
    {
      System.err.println("Invalid number of arguments provided.");
    }

    System.out.println("Result: " + result);
  }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM