要求:
1.增加一个初始化到最初状态的功能
2.增加一个退出程序的功能
3.对除法中除以0做一下处理
4.怎么把带小数点的运算增加进来(重要)
能不能在原来的程序上改动来达到以上的功能啊(JDK上运行)
ps:我是新手所以程序可能写的有点麻烦,有些方法可能比较笨,望大家多多指教啊!
test1.java
import java.io.*; public class test1{
public String ReadLine()
{
try{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
return br.readLine();
}
catch(Exception ex)
{
return null;
}
}
public boolean isInteger(String str)
{
try{
Integer.parseInt(str);
return true;
}
catch(Exception ex)
{
return false;
}
}
public int inPut()
{
System.out.print("请输入一个数据:");
String tmp=ReadLine();
while(!isInteger(tmp))
{
System.out.print("输入错误请重新输入:");
tmp=ReadLine();
}
int data=Integer.parseInt(tmp);
return data;
}
public void main(String args[])
{
}
}
fui.java
import java.io.*;
public class fui
{
static char ch;
public char caoZ()throws IOException
{
InputStreamReader ir;
BufferedReader in;
ir=new InputStreamReader(System.in);
in=new BufferedReader(ir);
System.out.println("请输入操作符:");
String s=in.readLine();
this.ch=s.charAt(0);
return ch;
}
public boolean yesorNO()throws IOException
{
switch(caoZ())
{
case '+':
return true;
case '-':
return true;
case '*':
return true;
case '/':
return true;
default:
return false;
}
}
public void main(String args[])
{
}}CalculatorA.java
import java.io.*;
public class CalculatorA
{
public static void main(String args[])throws IOException
{
test1 a=new test1();
fui b=new fui();
int z;
int x= a.inPut();
//char h=b.caoZ();
while(b.yesorNO()) {
switch(b.ch)
{
case '+':
z=x+a.inPut();
System.out.println("运算结果:"+z);
x=z;
break;
case '-':
z=x-a.inPut();
System.out.println("运算结果:"+z);
x=z;
break;
case '*':
z=x*a.inPut();
System.out.println("运算结果:"+z);
x=z;
break;
case '/':
z=x/a.inPut();
System.out.println("运算结果:"+z);
x=z;
break;
default:
System.out.println("输入错误:");
}
}
}
}
1.增加一个初始化到最初状态的功能
2.增加一个退出程序的功能
3.对除法中除以0做一下处理
4.怎么把带小数点的运算增加进来(重要)
能不能在原来的程序上改动来达到以上的功能啊(JDK上运行)
ps:我是新手所以程序可能写的有点麻烦,有些方法可能比较笨,望大家多多指教啊!
test1.java
import java.io.*; public class test1{
public String ReadLine()
{
try{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
return br.readLine();
}
catch(Exception ex)
{
return null;
}
}
public boolean isInteger(String str)
{
try{
Integer.parseInt(str);
return true;
}
catch(Exception ex)
{
return false;
}
}
public int inPut()
{
System.out.print("请输入一个数据:");
String tmp=ReadLine();
while(!isInteger(tmp))
{
System.out.print("输入错误请重新输入:");
tmp=ReadLine();
}
int data=Integer.parseInt(tmp);
return data;
}
public void main(String args[])
{
}
}
fui.java
import java.io.*;
public class fui
{
static char ch;
public char caoZ()throws IOException
{
InputStreamReader ir;
BufferedReader in;
ir=new InputStreamReader(System.in);
in=new BufferedReader(ir);
System.out.println("请输入操作符:");
String s=in.readLine();
this.ch=s.charAt(0);
return ch;
}
public boolean yesorNO()throws IOException
{
switch(caoZ())
{
case '+':
return true;
case '-':
return true;
case '*':
return true;
case '/':
return true;
default:
return false;
}
}
public void main(String args[])
{
}}CalculatorA.java
import java.io.*;
public class CalculatorA
{
public static void main(String args[])throws IOException
{
test1 a=new test1();
fui b=new fui();
int z;
int x= a.inPut();
//char h=b.caoZ();
while(b.yesorNO()) {
switch(b.ch)
{
case '+':
z=x+a.inPut();
System.out.println("运算结果:"+z);
x=z;
break;
case '-':
z=x-a.inPut();
System.out.println("运算结果:"+z);
x=z;
break;
case '*':
z=x*a.inPut();
System.out.println("运算结果:"+z);
x=z;
break;
case '/':
z=x/a.inPut();
System.out.println("运算结果:"+z);
x=z;
break;
default:
System.out.println("输入错误:");
}
}
}
}
package my_calculator;import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;/**
* 一个简单的计算器的实现
*
*/
public class SimpleCalculator implements ActionListener
{
//显示文本框
private JTextField tfDisplay;
//定义两个Panel组件:
// 1、用来放置撤销,清零等按钮操作
// 2、用来放置计算器的基本按钮,如1,2,3 ...
private JPanel optPanel, basePanel;
//定义基本操作按钮:撤销,清零,启动
private JButton backBtn, clearBtn, startBtn;
//定义一个按钮数组来存放基本数字
private JButton[] numBtn;
//定义加,减,乘,除以及等于按钮
private JButton addBtn, minusBtn, mulBtn, divBtn, resultBtn;
//定义窗口组件JFrame
private static JFrame jFrame;
//两个操作数
private int num1, num2;
//操作符号标志
private int optFlag;
private String msg;
//填充JFrame组件
public void load(){
jFrame = new JFrame("简单计算器");
jFrame.setLayout(new BorderLayout(10, 10));
//添加显示文本框
tfDisplay = new JTextField("请点击 Start 开始计算...");
tfDisplay.setHorizontalAlignment(SwingConstants.RIGHT);
tfDisplay.setEditable(false);
// tfDisplay.setEnabled(false);
jFrame.add(tfDisplay, "North");
//撤销上一步操作
backBtn = new JButton("Back");
backBtn.setForeground(Color.green);
backBtn.addActionListener(this);
//清零
clearBtn = new JButton("Clear");
clearBtn.setForeground(Color.green);
clearBtn.addActionListener(this);
//启动计算器
startBtn = new JButton("Start");
startBtn.setForeground(Color.green);
startBtn.addActionListener(this);
//添加操作按钮
JLabel jl = new JLabel("请购买正版!");
optPanel = new JPanel();
optPanel.add(jl);
optPanel.add(backBtn);
optPanel.add(clearBtn);
optPanel.add(startBtn);
jFrame.add(optPanel, "Center");
//初始化并添加基本按钮
basePanel = new JPanel(new GridLayout(0, 3 ,5, 5));
numBtn = new JButton[10];
for(int i = 0; i < numBtn.length; i++){
numBtn[i] = new JButton(String.valueOf(i));
numBtn[i].addActionListener(this);
basePanel.add(numBtn[i]);
}
//初始化基本运算符
addBtn = new JButton("+");
addBtn.setForeground(Color.red);
addBtn.addActionListener(this);
basePanel.add(addBtn);
minusBtn = new JButton("-");
minusBtn.setForeground(Color.red);
minusBtn.addActionListener(this);
basePanel.add(minusBtn);
mulBtn = new JButton("x");
mulBtn.setForeground(Color.red);
mulBtn.addActionListener(this);
basePanel.add(mulBtn);
divBtn = new JButton("÷");
divBtn.setForeground(Color.red);
divBtn.addActionListener(this);
basePanel.add(divBtn);
resultBtn = new JButton("=");
resultBtn.setForeground(Color.green);
resultBtn.addActionListener(this);
basePanel.add(resultBtn);
jFrame.add(basePanel, "South");
jFrame.setLocation(400, 300);
jFrame.pack();
jFrame.setVisible(true);
jFrame.setResizable(false);
}
public static void main(String[] args)
{
new SimpleCalculator().load();
//正常关闭窗口
jFrame.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
System.exit(0);
}
});
} @Override
public void actionPerformed(ActionEvent e)
{
double result = 0;
JButton be = (JButton)e.getSource();
String str = be.getText();
if(str.equals("+") || str.equals("-") || str.equals("x") || str.equals("÷")){
msg = tfDisplay.getText() + " " + str;
num1 = Integer.parseInt(tfDisplay.getText());
tfDisplay.setText("");
//判断究竟是哪个运算符
if(str.equals("+")) optFlag = 1;
if(str.equals("-")) optFlag = 2;
if(str.equals("x")) optFlag = 3;
if(str.equals("÷")) optFlag = 4;
}
else if(str.equals("=")){
num2 = Integer.parseInt(tfDisplay.getText());
msg = msg + " " + tfDisplay.getText() + " " + str;
if(optFlag == 1) result = num1 + num2;
if(optFlag == 2) result = num1 - num2;
if(optFlag == 3) result = num1 * num2;
if(optFlag == 4) result = num1 * 1.0 / num2;
tfDisplay.setText(msg + " " + String.valueOf(result));
}
else {
if (str.equals("Start"))
tfDisplay.setText("");
else
if (str.equals("Clear"))
tfDisplay.setText("0");
else
if (str.equals("Back")){
int end = tfDisplay.getText().length();
tfDisplay.setText(tfDisplay.getText().substring(0, end - 1));
}
else
tfDisplay.setText(tfDisplay.getText() + str);
}
}}