刚好用设计模式(简单工厂模式)编写了一个计算器。
package com.medavis.simplefactory.ui;
import java.util.Scanner;
import com.medavis.simplefactory.operate.OperatorFactory;
public class CaculatorUI {
private static Scanner sc;
public static void main(String[] args) {
try {
System.out.println("Please input a number:");
sc = new Scanner(System.in);
long first = sc.nextLong();
System.out.println("Please input a operational character (+-*/):");
sc = new Scanner(System.in);
String operator = sc.nextLine();
System.out.println("Please input a number:");
sc = new Scanner(System.in);
long second = sc.nextLong();
OperatorFactory opf=new OperatorFactory();
System.out.println(first + operator + second + "=" + opf.getOperator(operator).getResult(first, second));
} catch(Exception e) {
System.out.println("Your input is wrong!");
}
}
}
package com.medavis.simplefactory.operate;
public class OperatorAdd extends Operator{
@Override
public long getResult(long first, long second) {
return first+second;
}
}
package com.medavis.simplefactory.operate;
public class OperatorDiv extends Operator{
@Override
public long getResult(long first, long second) {
return first/second;
}
}
package com.medavis.simplefactory.operate;
public class OperatorFactory {
public Operator getOperator(String operator) throws Exception{
switch(operator){
case "+":
return new OperatorAdd();
case "-":
return new OperatorSub();
case "*":
return new OperatorMul();
case "/":
return new OperatorDiv();
default:
throw new Exception();
}
}
}
package com.medavis.simplefactory.operate;
public class OperatorMul extends Operator {
@Override
public long getResult(long first, long second) {
return first*second;
}
}
package com.medavis.simplefactory.operate;
public class OperatorSub extends Operator{
@Override
public long getResult(long first, long second) {
return first-second;
}
}
可以用if else语句
用switch更简单,代码也清晰,用switch吧
声明一个表示运算符号的变量(op)
switch(op){
case:"+"
...
break;
}
就这样写吧
import java.awt.*;
import java.awt.event.*;
class JiSuanQi implements ActionListener {
Frame f = new Frame("计算器");
Panel p = new Panel();
Panel p1 = new Panel();
Panel p2 = new Panel();
Button b = new Button("x+y");
Button b1 = new Button("x-y");
Button b2 = new Button("清除");
Label Lbx = new Label("x");
Label lby = new Label("y");
TextField tfx = new TextField(4);
TextField tfy = new TextField(4);
TextArea ta = new TextArea();
Font ft = new Font("宋体", Font.BOLD, 18);
GridLayout g1 = new GridLayout(4, 1);
GridLayout g2 = new GridLayout(4, 1);
double x, y, sum;
public static void main(String[] args) {
JiSuanQi ji = new JiSuanQi();
ji.go();
}
public void go() {
b.setFont(ft);
b1.setFont(ft);
b2.setFont(ft);
Lbx.setAlignment(Label.CENTER); //Lbx
lby.setAlignment(Label.CENTER);
Lbx.setFont(ft);
lby.setFont(ft);
tfx.setFont(ft);
tfy.setFont(ft);
ta.setFont(ft);
p.setLayout(g1);
p.add(Lbx);
p.add(tfx);
p.add(lby);
p.add(tfy);
f.add(p, "West");
p1.setLayout(g2);
p1.add(b);
p1.add(b1);
b.addActionListener(this);
b1.addActionListener(this);
f.add(p1, "East");
p2.add(b2);
b2.addActionListener(this);
f.add(p2, "South");
f.add(ta, "Center");
f.setSize(250, 150);
f.setVisible(true);
}
public void actionPerformed(ActionEvent ae) {
String s = ae.getActionCommand();
x = Double.parseDouble(tfx.getText());
y = Double.parseDouble(tfy.getText());
if (s.equals("x+y") == true) {
ta.append("x+y" + (x + y) + "\n");
}
if (s.equals("x-y") == true) {
ta.append("x-y" + (x - y) + "\n");
}
if (s.equals("清除") == true) {
ta.setText("");
}
}