dhgdl 2022-09-02 22:41 采纳率: 100%
浏览 13
已结题

java怎么加一个判定算术优先级的条件

package CalculatorImplementation;

public class Calculator {
private String brand;
private String[] pressButton;
private int pressButtonIndex;

public Calculator(){
    pressButton = new String[100];
    pressButtonIndex = 0;
}
public void setBrand(String brand){
    this.brand = brand;
}
public String getBrand(){
    return brand;
}
public void pressOne(){
    pressButton[pressButtonIndex] = "1";
    pressButtonIndex++;
    System.out.println("按了数字1");
}
public void pressTwo(){
    pressButton[pressButtonIndex] = "2";
    pressButtonIndex++;
    System.out.println("按了数字2");
}
public void pressThree(){
    pressButton[pressButtonIndex] = "3";
    pressButtonIndex++;
    System.out.println("按了数字3");
}
public void pressFour(){
    pressButton[pressButtonIndex] = "4";
    pressButtonIndex++;
    System.out.println("按了数字4");
}
public void pressFive(){
    pressButton[pressButtonIndex] = "5";
    pressButtonIndex++;
    System.out.println("按了数字5");
}public void pressSix(){
    pressButton[pressButtonIndex] = "6";
    pressButtonIndex++;
    System.out.println("按了数字6");
}public void pressSeven(){
    pressButton[pressButtonIndex] = "7";
    pressButtonIndex++;
    System.out.println("按了数字7");
}public void pressEight(){
    pressButton[pressButtonIndex] = "8";
    pressButtonIndex++;
    System.out.println("按了数字8");
}public void pressNine(){
    pressButton[pressButtonIndex] = "9";
    pressButtonIndex++;
    System.out.println("按了数字9");
}
public void pressAdd(){
    pressButton[pressButtonIndex] = "+";
    pressButtonIndex++;
    System.out.println("按了+");
}
public void pressSubtract(){
    pressButton[pressButtonIndex] = "-";
    pressButtonIndex++;
    System.out.println("按了-");
}
public void pressMultiply(){
    pressButton[pressButtonIndex] = "*";
    pressButtonIndex++;
    System.out.println("按了*");
}
public void pressDivide(){
    pressButton[pressButtonIndex] = "/";
    pressButtonIndex++;
    System.out.println("按了/");
}
public int pressEquals(){
    int result = 0;
    String flag = null;
    for(int i = 0;i < pressButtonIndex;i++){
        if(!pressButton[i].equals("+") && !pressButton[i].equals("-") && !pressButton[i].equals("*") && !pressButton[i].equals("/")){
            if(result == 0){
                result = Integer.parseInt(pressButton[i]);
            }else{
                if(flag.equals("+")){
                     result = result + Integer.parseInt(pressButton[i]);
                }else if(flag.equals("-")){
                     result = result - Integer.parseInt(pressButton[i]);
                }else if(flag.equals("*")){
                     result = result * Integer.parseInt(pressButton[i]);
                }else if(flag.equals("/")){
                     result = result / Integer.parseInt(pressButton[i]);
                }
            }
        }else{
            if(pressButton[i].equals("+")) {
                flag = "+";
            }else if(pressButton[i].equals("-")){
                flag = "-";
            }else if(pressButton[i].equals("*")){
                flag = "*";
            }else if(pressButton[i].equals("/")){
                flag = "/";
            }
        }
    }
    return result;
}

}

  • 写回答

1条回答 默认 最新

  • 陈言必行 Unity领域优质创作者 2022-09-03 08:51
    关注

    可以使用有限状态机,推荐参考:
    https://blog.csdn.net/Czhenya/article/details/78067542

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

问题事件

  • 系统已结题 9月28日
  • 已采纳回答 9月20日
  • 创建了问题 9月2日

悬赏问题

  • ¥15 VB.NET操作免驱摄像头
  • ¥15 笔记本上移动热点开关状态查询
  • ¥85 类鸟群Boids——仿真鸟群避障的相关问题
  • ¥15 CFEDEM自带算例错误,如何解决?
  • ¥15 有没有会使用flac3d软件的家人
  • ¥20 360摄像头无法解绑使用,请教解绑当前账号绑定问题,
  • ¥15 docker实践项目
  • ¥15 利用pthon计算薄膜结构的光导纳
  • ¥15 海康hlss视频流怎么播放
  • ¥15 Paddleocr:out of memory error on GPU