1

我必须用java编写简单的计算器。我在 Eclipse 中编写了我的代码,并且在 dodaj 和 odejmij 方法中有两个错误(相同)。

Eclipse 中的错误 -insert "EnumBody" to complete BlockStatement

我寻找如何解决它,但我没有找到。请帮我。有人有同样的问题吗?非常感谢帮助我。

    import java.util.Scanner;
    class Kalkulator {
    static Object z1=new LiczbaZespolona();
    static Object z2=new LiczbaZespolona();
    String dodaj="+";
    String odejmij="-";
    String pomnoz="*";
    String podziel="/";
    String wynik;
    static String d;
    String dzialanie=d;
    public static void main(String args[]){

        Scanner input = new Scanner(System.in);
        System.out.println("Podaj pierwszą liczbę: ");
        z1 = input.next();

        System.out.println("Podaj drugą liczbę: ");
        z2 =  input.next();

        System.out.println("wybierz działanie: ");
         d = input.next();


        public static dodaj();{//insert "EnumBody" to complete BlockStatement, insert "enum Identifier" to complete EnumHeaderName


                 z3=z1+z2;
                return;
            }
             public static odejmij();{//insert "EnumBody" to complete BlockStatement, insert "enum Identifier" to complete EnumHeaderName

                z4=z1-z2;
                return;
            }



            switch (wynik){
            case 1:
                if (d=='+'){
                    return dodaj;}
                    break;
            case 2:
                if(d=='-'){
                    return odejmij;
                }
                break;
            }   
        }
}
4

3 回答 3

0

除了 SO 用户写的问题之外,您正在做的算术也会有更多问题。由于 Java 不支持运算符重载。这对您来说意味着如果您有两个类对象LiczbaZespolonaComplexNumber),编译器将不知道如何处理它并且将无法编译。

为了解决这个问题,你需要两件事在某个类中实现这些数字的算术。当然,您需要一些点才能开始,这个界面可能会有所帮助

private static interface IComplexNumber {

        public IComplexNumber add(IComplexNumber complexNumber);
        public IComplexNumber substract(IComplexNumber complexNumber);
        public IComplexNumber multiply(IComplexNumber complexNumber);
        public IComplexNumber divide(IComplexNumber complexNumber);

}

然后,当您知道如何添加两个 ComplexNumber 时,您将需要一些可以为您执行操作的东西。对于这种类型的操作,最好的方法是枚举类型。

这门课可以从这个开始

private enum CompleNumberCalculation {
    ADD('+') {

        @Override
        protected IComplexNumber operation(IComplexNumber left, IComplexNumber right) {
            return left.add(right);
        }
    };

    private char operator;

    private CompleNumberCalculation(char operator) {
        this.operator = operator;
    }

    protected abstract IComplexNumber operation(IComplexNumber left, IComplexNumber right);


}

为了帮助你,我写了一个简单的MathOperation枚举来解决RegularNumber的计算

public enum RegularMathOperation {
    ADD('+') {

        @Override
        protected BigDecimal operation(BigDecimal left, BigDecimal right) {
            return left.add(right);
        }
    },
    SUBSTRACT('-') {

        @Override
        protected BigDecimal operation(BigDecimal left, BigDecimal right) {
            return left.subtract(right);
        }
    },
    MULTIPLY('*') {

        @Override
        protected BigDecimal operation(BigDecimal left, BigDecimal right) {
            return left.multiply(right);
        }
    },
    DIVIDE('/') {

        @Override
        protected BigDecimal operation(BigDecimal left, BigDecimal right) {
            return left.divide(right,MathContext.DECIMAL64);
        }
    };

    private final char operator;

    private RegularMathOperation(char operator) {
        this.operator = operator;
    }

    public char operator() {
        return this.operator;
    }

    public <T1 extends Number, T2 extends Number> T1 calculate(T1 left, T2 right) {

        validateInput(left, right);

        Class<? extends Number> resultType = left.getClass();
        BigDecimal result = this.operation(toBigDecimal(left), toBigDecimal(right));

        return (T1) toType(resultType, result);

    }

    protected abstract BigDecimal operation(BigDecimal left, BigDecimal right);

    private void validateInput(Number left, Number right) {

        if(left == null) {
            throw new IllegalArgumentException("Value of left argument must not be null to perform operation " + this.name());
        }

        if(right == null) {
            throw new IllegalArgumentException("Value of left argument must not be null to perform operation " + this.name());
        }

    }

    private BigDecimal toBigDecimal(Number value) {

        if(value instanceof BigDecimal) {
            return (BigDecimal) value;
        }

        return new BigDecimal(value.doubleValue());

    }

    private Number toType(Class<? extends Number> type, BigDecimal value) {

        if(Double.class.isAssignableFrom(type)) {
            return type.cast(value.doubleValue());
        }

        if(Long.class.isAssignableFrom(type)) {
            return type.cast(value.longValue());
        }

        if(Integer.class.isAssignableFrom(type)) {
            return type.cast(value.intValue());
        }

        //...

        throw new IllegalStateException("Type not support: "+type);

    }

    public static Map<Character, RegularMathOperation> operatorMap = new HashMap<Character, RegularMathOperation>();


    static  {//Fill map

        for(RegularMathOperation mathOperation : RegularMathOperation.values()) {
            operatorMap.put(mathOperation.operator(), mathOperation);
        }

    }

    public static RegularMathOperation valueOf(char operator) {

        RegularMathOperation mathOperation = operatorMap.get(Character.valueOf(operator));

        if(mathOperation == null) {
            throw new IllegalArgumentException("Could not find MathOperator for operartor: " + operator);
        }

        return mathOperation;
    }

    public static <T1 extends Number, T2 extends Number> T1 resultOf(char operator, T1 left, T2 right) {

        return RegularMathOperation.valueOf(operator).calculate(left, right);

    }


    public static void main(String[] args) {

        Long left   = 3L;
        Double right  = 2D;


        System.out.println(RegularMathOperation.resultOf('+', left, right));
        System.out.println(RegularMathOperation.resultOf('-', left, right));

        System.out.println(RegularMathOperation.valueOf('*').calculate(left, right));
        System.out.println(RegularMathOperation.valueOf('*').calculate( left, right));

        System.out.println(RegularMathOperation.DIVIDE.calculate(left, right));
        System.out.println(RegularMathOperation.DIVIDE.calculate(right,left));

    }

}
于 2012-10-15T16:13:05.733 回答
0
  1. 从和中删除分号 ( ;) 。public static dodaj();{public static odejmij();{

  2. 将它们移出main

  3. 添加Object为返回类型。

  4. Returnz3z4from 方法。

  5. 在 main 中,您正在调用dodajand odejmij,添加()括号

  6. 我不确定您的LiczbaZespolona,因此无法评论+正在使用的运算符,但这听起来不合适。

最后它应该看起来像:

public static void main(String args[]){

   Scanner input = new Scanner(System.in);
   System.out.println("Podaj pierwszą liczbę: ");
   z1 = input.next();

   System.out.println("Podaj drugą liczbę: ");
   z2 =  input.next();

   System.out.println("wybierz działanie: ");
   d = input.next();


    switch (wynik){
    case 1:
        if (d=='+'){
            return dodaj();
         }
         break;
    case 2:
        if(d=='-'){
            return odejmij();
        }
        break;
    }   
  }

    public static Object dodaj(){
         Object z3=z1+z2;
         return z3;
    }

    public static Object odejmij(){
        Object z4=z1-z2;
        return z4;
    }
于 2012-10-15T15:00:56.647 回答
0

1) 公共静态 dodaj();{

删除冒号。当您添加冒号时,它将被视为abstract方法。

public static dodaj(){

2)将您的方法移动到main类主体的外部和内部。

于 2012-10-15T14:59:33.307 回答