呆呆的計(jì)算器(version 1.0)
這段代碼是來(lái)自大話,小菜最開始的面試代碼。小菜是一個(gè)小白轩娶。嗯,小菜果然是個(gè)小白框往。小菜好白鳄抒。
public class Program {
public static void main(String[] args) {
int result = 0;
System.out.println("請(qǐng)輸入數(shù)字A");
String a = "123";
System.out.println("請(qǐng)選擇運(yùn)算符號(hào)(+、-椰弊、*许溅、/):");
String b = "+";
System.out.println("請(qǐng)輸入數(shù)字B");
String c = "321";
String d = "";
if (b.equals("+"))
result = Integer.parseInt(a) + Integer.parseInt(c);
c = result + "";
System.out.println(c);
}
}
呆呆計(jì)算器(version 2.0)
老鳥說(shuō)小菜,寫的代碼不規(guī)范秉版。不是一個(gè)合格的牛馬贤重。然后小菜勵(lì)志要做一個(gè)合格的牛馬,然后改了一版清焕。
規(guī)范了命名并蝗。
public class Program2 {
public static void main(String[] args) {
System.out.println("請(qǐng)輸入數(shù)字A");
String strNumberA = "123";
System.out.println("請(qǐng)選擇運(yùn)算符號(hào)(+、-秸妥、*滚停、/):");
String strOperate = "+";
System.out.println("請(qǐng)輸入數(shù)字B");
String strNumberB = "321";
String strResult = "";
int result = 0;
switch (strOperate) {
case "+" :
result = Integer.parseInt(strNumberA) + Integer.parseInt(strNumberB);
break;
case "-" :
result = Integer.parseInt(strNumberA) - Integer.parseInt(strNumberB);
break;
case "*" :
result = Integer.parseInt(strNumberA) * Integer.parseInt(strNumberB);
break;
case "/" :
if (!strNumberB.equals("0"))
result = Integer.parseInt(strNumberA) / Integer.parseInt(strNumberB);
else
strResult = "除數(shù)不能為0";
break;
}
System.out.print("結(jié)果是 :" + result);
System.out.println(strResult);
}
}
呆呆計(jì)算器(version 3.0)
把邏輯計(jì)算代碼與用戶界面代買分離
public class Operation {
public static double getResult(double numberA, double numberB, String operate) {
double result = 0;
switch (operate) {
case "+" : result = numberA + numberB;
break;
case "-" : result = numberA - numberB;
break;
case "*" : result = numberA * numberB;
break;
case "/" : result = numberA / numberB;
break;
}
return result;
}
}
public class Program3 {
public static void main(String[] args) {
System.out.println("請(qǐng)輸入數(shù)字A");
String strNumberA = "123";
System.out.println("請(qǐng)選擇運(yùn)算符號(hào)(+、-粥惧、*键畴、/):");
String strOperate = "+";
System.out.println("請(qǐng)輸入數(shù)字B");
String strNumberB = "321";
String strResult = "";
strResult =
"" + (Operation.getResult(Double.parseDouble(strNumberA), Double.parseDouble(strNumberB), strOperate));
System.out.print("結(jié)果是 :" + strResult);
System.out.println(strResult);
}
}
呆呆計(jì)算器(version 4.0) 大招來(lái)了
基類將不變的東西放在基類,將變化的東西放在子類突雪。
public abstract class Operation2 {
public double numberA = 0;
public double numberB = 0;
public double result = 0;
public double getNumberA() {
return numberA;
}
public void setNumberA(double numberA) {
this.numberA = numberA;
}
public double getNumberB() {
return numberB;
}
public void setNumberB(double numberB) {
this.numberB = numberB;
}
public abstract double getResult_();
}
加法計(jì)算子類
public class OperationAdd extends Operation2 {
@Override
public double getResult_() {
result = numberA + numberB;
return result;
}
}
減法計(jì)算子類
public class OperationSub extends Operation2 {
@Override
public double getResult_() {
result = numberA - numberB;
return result;
}
}
乘法計(jì)算子類
public class OperationMul extends Operation2 {
@Override
public double getResult_() {
result = numberA * numberB;
return result;
}
}
除法計(jì)算子類
public class OperationDiv extends Operation2 {
@Override
public double getResult_() {
if (numberB != 0)
result = numberA / numberB;
else
// throw new Exception("除數(shù)不能為0");
System.out.println("除數(shù)不能為0");
return result;
}
}
工廠類 工廠類 工廠糞
public class OperationFactory {
public static Operation2 operation = null;
public static Operation2 createOperation(String opera) {
switch (opera) {
case "+" : operation = new OperationAdd();
break;
case "-" : operation = new OperationSub();
break;
case "*" : operation = new OperationMul();
break;
case "/" : operation = new OperationDiv();
break;
}
return operation;
}
Please see the magic
public class Program4 {
public static void main(String[] args) {
Operation2 operation = null;
operation = OperationFactory.createOperation("+");
operation.setNumberA(123);
operation.setNumberB(321);
System.out.println("" + operation.getResult_());
}
}
真他么簡(jiǎn)潔起惕,這就是藝術(shù)!This is art.