我必须在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;
}
}
}
答案 0 :(得分:0)
1) public static dodaj(); {
删除冒号。添加冒号时,它将被视为abstract
方法。
public static dodaj(){
2)将你的方法移到main
之外和课堂体内。
答案 1 :(得分:0)
从;
和public static dodaj();{
删除分号(public static odejmij();{
)。
将其移出main
添加Object
作为返回类型。
从方法中返回z3
和z4
。
主要是您致电dodaj
和odejmij
的地方,添加()
括号
我不确定您的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;
}
答案 2 :(得分:0)
除了SO用户编写的问题之外,您将在运算中遇到更多问题。 As Java do not support operator overload。这对你意味着什么,如果你有两个类LiczbaZespolona
(ComplexNumber)的对象,编译器将不知道如何处理它并且将无法编译。
要解决这个问题,你需要在一些类中实现这些数字的算术。当然,您需要一些启动点,这个界面可能会有所帮助
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));
}
}