import java.util.Scanner
public class Zhidao {
public static void main(String[] args) {
String condition = ""
Zhidao zhidao = new Zhidao()
do{
Scanner scanner = new Scanner(System.in)
try{
System.out.print("请输入第一个数:")
double x = scanner.nextDouble()
System.out.print("请输入第二个数:")
double y = scanner.nextDouble()
System.out.print("请输入运算符:")
String s = scanner.next()
char z = s.charAt(0)
zhidao.yunsuan(x, y, z)
}catch(Exception e){
System.out.println("请输入正确的数据!")
}
System.out.print("是否继续?continue:继续,任意字符:结束")
condition = scanner.next()
}while("continue".equals(condition))
}
public static void yunsuan(double x,double y,Character z){
DecimalFormat r=new DecimalFormat()
r.applyPattern("#0.00")
if(z.equals('+')){
System.out.println(x+"+"+y+"=" + r.format((x+y)))
} else if(z.equals('-')){
System.out.println(x+"-"+y+"=" + r.format((x-y)))
} else if(z.equals('*')){
System.out.println(x+"*"+y+"=" + r.format((x*y)))
} else if(z.equals('/')){
if(y==0){
System.out.println("被除数不能为0")
} else{
System.out.println(x+"/"+y+"=" + r.format((x/y)))
}
}else{
System.out.println("无法识别改运算符")
}
}
}
(首先建个类,把这些复制粘贴进去)import java.awt.*
import javax.swing.*
public class F {
JFrame frame = new JFrame("计算机")
JPanel pl = new JPanel()
JPanel p2 = new JPanel()
static JTextField show = new JTextField()
static JButton b0 = new JButton("0")
static JButton b1 = new JButton("1")
static JButton b2 = new JButton("2")
static JButton b3 = new JButton("3")
static JButton b4 = new JButton("4")
static JButton b5 = new JButton("5")
static JButton b6 = new JButton("6")
static JButton b7 = new JButton("7")
static JButton b8 = new JButton("8")
static JButton b9 = new JButton("9")
JButton bjia = new JButton("+")
JButton bjian = new JButton("-")
JButton bcheng = new JButton("*")
JButton bchu = new JButton("/")
JButton bdian = new JButton(".")
JButton bdeng = new JButton("=")
JButton bqingchu = new JButton("清除")
public void y() {
pl.setLayout(new GridLayout(1, 1))
pl.add(show)
}
public void p() {
b1.addActionListener(new U())
b2.addActionListener(new U())
b3.addActionListener(new U())
b4.addActionListener(new U())
b5.addActionListener(new U())
b6.addActionListener(new U())
b7.addActionListener(new U())
b8.addActionListener(new U())
b9.addActionListener(new U())
b0.addActionListener(new U())
bjia.addActionListener(new Fu())
bjian.addActionListener(new Fu())
bcheng.addActionListener(new Fu())
bchu.addActionListener(new Fu())
bdeng.addActionListener(new Deng())
bqingchu.addActionListener(new Qing())
p2.setLayout(new GridLayout(6, 3))
p2.add(b1)
p2.add(b2)
p2.add(b3)
p2.add(b4)
p2.add(b5)
p2.add(b6)
p2.add(b7)
p2.add(b8)
p2.add(b9)
p2.add(b0)
p2.add(bjia)
p2.add(bjian)
p2.add(bcheng)
p2.add(bchu)
p2.add(bdian)
p2.add(bqingchu)
p2.add(bdeng)
}
public void o() {
frame.setLayout(new BorderLayout())
frame.add(pl, BorderLayout.NORTH)
frame.add(p2, BorderLayout.CENTER)
frame.setSize(400, 300)
frame.setVisible(true)
}
public static void main(String[] args) {
F f = new F()
f.y()
f.p()
f.o()
}
}
(再新建个类 把这些也复制粘贴进去)
import java.awt.event.ActionEvent
import java.awt.event.ActionListener
public class U implements ActionListener {
public static String str = ""
public static String a = ""
public static String b = ""
public static String k = ""
public void actionPerformed(ActionEvent e) {
String w = e.getActionCommand()//字
if (k.equals("")) {
a += w
F.show.setText(a)
} else {
b += w
F.show.setText(b)
}
}
}
(再新建一个,把下面的复制粘贴)
import java.awt.event.ActionEvent
import java.awt.event.ActionListener
public class Deng implements ActionListener {
public void actionPerformed(ActionEvent e) {
int a = Integer.parseInt(U.a)
int b = Integer.parseInt(U.b)
int c = 0
if (U.k.equals("+")) {
c = a + b
} else
if (U.k.equals("-")) {
c = a - b
} else
if (U.k.equals("*")) {
c = a * b
} else
if (U.k.equals("/")) {
c = a / b
} else {
}
String d = String.valueOf(c)
F.show.setText(d)
U.a = d
U.b = ""
U.k = ""
}
}
(在建一个 复制粘贴)
import java.awt.event.ActionEvent
import java.awt.event.ActionListener
public class Fu implements ActionListener {
public void actionPerformed(ActionEvent e) {
String a = e.getActionCommand()//字
U.k = a
}
}
(在建一个)
import java.awt.event.ActionEvent
import java.awt.event.ActionListener
public class Qing implements ActionListener {
public void actionPerformed(ActionEvent e) {
U.a = ""
U.b = ""
U.k = ""
F.show.setText("")
}
}
有一个现成的.你看看吧import java.awt.event.ActionEvent
public class Application extends JFrame {
protected String str = ""
protected boolean isChar = true
protected boolean isEqual = false
protected JTextField textField
public Application() {
Listener listerner = new Listener(this)
getContentPane().setLayout(null)
JButton button = new JButton("7")
button.addActionListener(listerner)
button.setBounds(12, 69, 43, 27)
getContentPane().add(button)
textField = new JTextField()
textField.setText("0")
textField.setEditable(false)
textField.setHorizontalAlignment(JTextField.RIGHT)
textField.setBounds(12, 22, 377, 27)
getContentPane().add(textField)
textField.setColumns(10)
JButton button_1 = new JButton("8")
button_1.addActionListener(listerner)
button_1.setBounds(103, 69, 43, 27)
getContentPane().add(button_1)
JButton button_2 = new JButton("9")
button_2.addActionListener(listerner)
button_2.setBounds(182, 69, 43, 27)
getContentPane().add(button_2)
JButton button_3 = new JButton("4")
button_3.addActionListener(listerner)
button_3.setBounds(12, 106, 43, 27)
getContentPane().add(button_3)
JButton button_4 = new JButton("5")
button_4.addActionListener(listerner)
button_4.setBounds(103, 106, 43, 27)
getContentPane().add(button_4)
JButton button_5 = new JButton("6")
button_5.addActionListener(listerner)
button_5.setBounds(182, 106, 43, 27)
getContentPane().add(button_5)
JButton button_6 = new JButton("1")
button_6.addActionListener(listerner)
button_6.setBounds(12, 143, 43, 27)
getContentPane().add(button_6)
JButton button_7 = new JButton("2")
button_7.addActionListener(listerner)
button_7.setBounds(103, 143, 43, 27)
getContentPane().add(button_7)
JButton button_8 = new JButton("3")
button_8.addActionListener(listerner)
button_8.setBounds(182, 143, 43, 27)
getContentPane().add(button_8)
JButton button_9 = new JButton("+")
button_9.addActionListener(listerner)
button_9.setBounds(269, 72, 43, 27)
getContentPane().add(button_9)
JButton button_10 = new JButton("-")
button_10.addActionListener(listerner)
button_10.setBounds(346, 72, 43, 27)
getContentPane().add(button_10)
JButton button_11 = new JButton("*")
button_11.addActionListener(listerner)
button_11.setBounds(269, 109, 43, 27)
getContentPane().add(button_11)
JButton button_12 = new JButton("/")
button_12.addActionListener(listerner)
button_12.setBounds(346, 109, 43, 27)
getContentPane().add(button_12)
JButton button_13 = new JButton("=")
button_13.addActionListener(listerner)
button_13.setBounds(346, 143, 43, 27)
getContentPane().add(button_13)
JButton button_14 = new JButton("0")
button_14.addActionListener(listerner)
button_14.setBounds(103, 180, 43, 27)
getContentPane().add(button_14)
JButton btnReset = new JButton("reset")
btnReset.addActionListener(listerner)
btnReset.setBounds(269, 180, 118, 27)
getContentPane().add(btnReset)
JButton button_15 = new JButton(".")
button_15.addActionListener(listerner)
button_15.setBounds(269, 146, 43, 27)
getContentPane().add(button_15)
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE)
this.setResizable(false)
this.setSize(442, 260)
this.setLocationRelativeTo(null)
this.setVisible(true)
}
public static void main(String[] args) {
new Application()
}
}
class Listener implements ActionListener {
private Application app = null
public Listener(Application app) {
this.app = app
}
public void actionPerformed(ActionEvent e) {
String value = e.getActionCommand()
if (value.matches("[0-9.]")) {
if (app.isChar) {
app.textField.setText("")
app.isChar = false
}
if (app.isEqual &&app.str.matches("[0-9.]*")) {
app.str = ""
app.isEqual = false
}
app.str += value
app.textField.setText(app.textField.getText() + value)
} else if (value.matches("[\\+\\-\\*/]")) {
if (!app.str.substring(app.str.length() - 1)
.matches("[\\+\\-\\*/]")) {
app.str += value
app.isChar = true
}
} else if ("=".equals(value)) {
app.isEqual = true
if (app.str.substring(app.str.length() - 1).matches("[\\+\\-]")) {
app.str += "0"
} else if (app.str.substring(app.str.length() - 1)
.matches("[\\*/]")) {
app.str += "1"
}
Interpreter bsh = new Interpreter()
String obj = null
try {
obj = bsh.eval(app.str).toString()
} catch (Exception exception) {
System.out.println(exception.getMessage())
}
System.out.println(app.str)
app.textField.setText(obj)
app.str = obj
app.isChar = true
} else {
app.str = ""
app.textField.setText("0")
}
}
}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)