-2

这是一个只使用 + 和 - 的简单算术运算计算器。逻辑部分很简单,但我很尴尬地说代码不起作用。输入的数字未知。例如:“4/2”或“42+34”都应该可以接受。我对字符串及其功能不太满意,所以请帮忙。谢谢!:)

/*
 *Calculator with only + and - 
 */
import java.util.Scanner;

class Calculator {
    public static String strng;

    public static void main(String[]args){
        Calculator calc = new Calculator();
        calc.Calculator();
    }

    public void Calculator()
    {
        int op1=0,op2=0,flag=0,index=0;
        String Operator=null;
        System.out.println("java calculator");
        Scanner sc=new Scanner(System.in);

        strng= sc.nextLine();

        if(strng.indexOf("+")>0) {
            Operator = "+";
            index=strng.indexOf("+");
            if(strng.lastIndexOf("+")!=index){
              Operator="++";
              flag=1;
            }
        }
        else if(strng.indexOf("-")>0) {
            Operator="-";
            index=strng.indexOf("-");
            if(strng.lastIndexOf("-")!=index){
              Operator="--";
              flag=1;
            }
        } else if(strng.indexOf("*")>0) {
            Operator="*";
            index=strng.indexOf("*");
        } else if(strng.indexOf("/")>0) {
            Operator="/";
            index=strng.indexOf("/");
        } 

        if((index>0)&&(flag==0)) {
           op1=Integer.parseInt(strng.substring(0,index-1));
           op2=Integer.parseInt(strng.substring(index+1));
           Operation(Operator,op1,op2);
        }

        if(flag==1) {
           op1=Integer.parseInt(strng.substring(0,index-1));
           op2=Integer.parseInt(strng.substring(index+2)); 
           Operation(Operator,op1,op2);
        } else { //to separate the operator and operands string functions.
           System.out.println("Invalid choice");
        }
     }  


     public void Operation(String Operator, int operand1, int operand2) {
        if(Operator.equals("+")){ Add(operand1,operand2); }
        if(Operator.equals("-")){ Subtract(operand1,operand2); }
        if(Operator.equals("*")){ Multiply(operand1,operand2); }
        if(Operator.equals("/")){ Divide(operand1,operand2); }
        if(Operator.equals("++")){ Increment(operand1); }
        if(Operator.equals("--")){
            Decrement(operand1);
        } else {
            System.out.println("Invalid entry");
        }
    } 


    void Add(int op1, int op2) {
        System.out.println(strng+"="+(op1+op2));
    }

    void Subtract(int op1, int op2) {
        System.out.println(strng+"="+(op1-op2));
    }  

    void Increment(int op1) {
        System.out.println(strng+"="+(op1+1));
    }

    void Decrement(int op1) {
        System.out.println(strng+"="+(op1-1));
    }

    void Multiply(int op1, int op2) {
        int ans=0;
        int  x = op1<op2?op1:op2;
        int y = op1>op2?op1:op2;
        for(int i=1;i<=x;i++){
            ans=ans+y;
        } 
        System.out.println(strng+"="+ans);
    }

    void Divide(int op1, int op2) {
        int count=0,rem=op1;

        while(rem>=op2){
            rem=rem-op2;
            count=count+1;
        } 
        System.out.println(strng+"="+count); //return rem for modulo operand
    }

}
4

2 回答 2

0

我对您在字符串中查找符号的方式感到不舒服。我建议您查看String.split(String regex),这将允许您拆分符号和数字。

于 2012-05-10T16:20:02.317 回答
-2

您正在调用构造函数...构造函数不支持 void .. 希望对您有所帮助

于 2012-05-10T16:19:19.807 回答