十年网站开发经验 + 多家企业客户 + 靠谱的建站团队
量身定制 + 运营维护+专业推广+无忧售后,网站问题一站解决
主要涉及的知识点: 类的写法, 以及方法的调用 .建议多做练习. 如果有看不懂的地方. 可以继续追问,一起讨论.
创新互联建站2013年开创至今,先为兴宁等服务建站,兴宁等地企业,进行企业商务咨询服务。为兴宁企业网站制作PC+手机+微官网三网同步一站式服务解决您的所有建站问题。
参考代码如下
//Number类
class Number {
private int n1;//私有的整型数据成员n1
private int n2;//私有的整型数据成员n2
// 通过构造函数给n1和n2赋值
public Number(int n1, int n2) {
this.n1 = n1;
this.n2 = n2;
}
// 加法
public int addition() {
return n1 + n2;
}
// 减法
public int subtration() {
return n1 - n2;
}
// 乘法
public int multiplication() {
return n1 * n2;
}
// 除法 (可能除不尽,所以使用double作为返回类型)
public double division() {
return n1 * 1.0 / n2; // 通过n1*1.0 把计算结果转换成double类型.
}
}
//Exam4 类
public class Exam4{
public static void main(String[] args) {
Number number=new Number(15, 6);//创建Number类的对象
//下面的是调用方法得到返回值进行输出显示
System.out.println("加法"+number.addition());
System.out.println("减法"+number.subtration());
System.out.println("乘法"+number.multiplication());
System.out.println("除法"+number.division());
}
}
public static void main(String[] args) {
System.out.println("简单计算器");
boolean flag=true;//while循环是否继续,true继续循环,false停止循环
System.out.println("请输入第一个数字");
while(flag){
Scanner scan = new Scanner(System.in);
String bh=scan.next();
try {
double num = Double.parseDouble(bh);
System.out.println("请输入符号(+、-、*、/)");
while(1==1){
String fh=scan.next();
if("+".equals(fh) || "-".equals(fh) || "*".equals(fh) || "/".equals(fh)){
System.out.println("请输入第二个数字");
while(1==1){
String bh2=scan.next();
try {
double num2 = Double.parseDouble(bh2);
double num3=0;
if("+".equals(fh)){
num3=num+num2;
}else if("-".equals(fh)){
num3=num-num2;
}else if("*".equals(fh)){
num3=num*num2;
}else if("/".equals(fh)){
num3=num/num2;
}
System.out.println(num3);
break;
} catch (Exception e) {
System.out.println("请输入第二个正确的数字");
continue;
}
}
break;
}else{
System.out.println("请输入正确的符号(+、-、*、/)");
continue;
}
}
System.out.println("是否继续运算:输入Y或者y继续,输入其它任意字符退出");
String yn=scan.next();
if("Y".equals(yn) || "y".equals(yn)){
continue;
}else{
System.out.println("运算结束");
break;
}
}catch (NumberFormatException e) {//输入非数字类型时
System.out.println("请输入第一个正确的数字");
continue;
}
}
}
试试下面的代码 绝对没有错误。
package main;
import java.awt.Button;
import java.awt.Color;
import java.awt.GridLayout;
import java.awt.Panel;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.math.BigDecimal;
import javax.swing.JFrame;
import javax.swing.JTextField;
public class app7 extends JFrame implements ActionListener {
static Panel pan = new Panel();
static JTextField textField = new JTextField("0");
static Button b0, b1, b2, b3, b4, b5, b6, b7, b8, b9, bp, ba, bs, bm, bd,
be, bc, bt, bf, bh;
private StringBuffer temp = new StringBuffer("");
private String optValue = "0";
private String optType="";
private boolean isChoiseOptType=true;
public void init() {
b0 = new Button("0");
b0.addActionListener(this);
b1 = new Button("1");
b1.addActionListener(this);
b2 = new Button("2");
b2.addActionListener(this);
b3 = new Button("3");
b3.addActionListener(this);
b4 = new Button("4");
b4.addActionListener(this);
b5 = new Button("5");
b5.addActionListener(this);
b6 = new Button("6");
b6.addActionListener(this);
b7 = new Button("7");
b7.addActionListener(this);
b8 = new Button("8");
b8.addActionListener(this);
b9 = new Button("9");
b9.addActionListener(this);
bp = new Button(".");
bp.addActionListener(this);
ba = new Button("+");
ba.addActionListener(this);
bs = new Button("-");
bs.addActionListener(this);
bm = new Button("*");
bm.addActionListener(this);
bd = new Button("/");
bd.addActionListener(this);
be = new Button("=");
be.addActionListener(this);
bc = new Button("c");
bc.addActionListener(this);
bt = new Button("退格");
bt.addActionListener(this);
bf = new Button("1/x");
bf.addActionListener(this);
bh = new Button("+/-");
bh.addActionListener(this);
this.setTitle("计算机");
this.setLayout(null);
this.setSize(260, 300);
this.setResizable(false);
GridLayout grid = new GridLayout(4, 5);
pan.setLayout(grid);
pan.setBounds(20, 60, 150, 120);
textField.setBounds(20, 35, 150, 20);
textField.setBackground(Color.cyan);
textField.setHorizontalAlignment(textField.RIGHT);
textField.setEditable(false);
pan.add(b1);
pan.add(b2);
pan.add(b3);
pan.add(ba);
pan.add(bc);
pan.add(b4);
pan.add(b5);
pan.add(b6);
pan.add(bs);
pan.add(bt);
pan.add(b7);
pan.add(b8);
pan.add(b9);
pan.add(bm);
pan.add(bf);
pan.add(b0);
pan.add(bh);
pan.add(bp);
pan.add(bd);
pan.add(be);
this.add(textField);
this.add(pan);
}
public static void main(String[] args) {
app7 frm = new app7();
frm.init();
frm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frm.setVisible(true);
}
@SuppressWarnings("static-access")
@Override
public void actionPerformed(ActionEvent e) {
String value="0";
if (e.getSource().equals(b0)) {
this.temp.append(b0.getLabel());
this.textField.setText(this.temp.toString());
isChoiseOptType=false;
} else if (e.getSource().equals(b1)) {
this.temp.append(b1.getLabel());
this.textField.setText(this.temp.toString());
isChoiseOptType=false;
} else if (e.getSource().equals(b2)) {
this.temp.append(b2.getLabel());
this.textField.setText(this.temp.toString());
isChoiseOptType=false;
} else if (e.getSource().equals(b3)) {
this.temp.append(b3.getLabel());
this.textField.setText(this.temp.toString());
isChoiseOptType=false;
} else if (e.getSource().equals(b4)) {
this.temp.append(b4.getLabel());
this.textField.setText(this.temp.toString());
isChoiseOptType=false;
} else if (e.getSource().equals(b5)) {
this.temp.append(b5.getLabel());
this.textField.setText(this.temp.toString());
isChoiseOptType=false;
} else if (e.getSource().equals(b6)) {
this.temp.append(b6.getLabel());
this.textField.setText(this.temp.toString());
isChoiseOptType=false;
} else if (e.getSource().equals(b7)) {
this.temp.append(b7.getLabel());
this.textField.setText(this.temp.toString());
isChoiseOptType=false;
} else if (e.getSource().equals(b8)) {
this.temp.append(b8.getLabel());
this.textField.setText(this.temp.toString());
isChoiseOptType=false;
} else if (e.getSource().equals(b9)) {
this.temp.append(b9.getLabel());
this.textField.setText(this.temp.toString());
isChoiseOptType=false;
} else if (e.getSource().equals(bp)) {
if(this.temp.length()=0)
this.temp.append("0");
this.temp.append(bp.getLabel());
this.textField.setText(this.temp.toString());
isChoiseOptType=false;
} else if (e.getSource().equals(ba)) {
if(!isChoiseOptType){
value=this.textField.getText();
if(value.lastIndexOf(".")==value.length()-1){
value=value.substring(0,value.length()-1);
}
this.optValue=value;
this.temp=new StringBuffer("");
}
this.optType=ba.getLabel();
isChoiseOptType=true;
} else if (e.getSource().equals(bs)) {
if(!isChoiseOptType){
value=this.textField.getText();
if(value.lastIndexOf(".")==value.length()-1){
value=value.substring(0,value.length()-1);
}
this.optValue=value;
this.temp=new StringBuffer("");
}
this.optType=bs.getLabel();
isChoiseOptType=true;
} else if (e.getSource().equals(bm)) {
if(!isChoiseOptType){
value=this.textField.getText();
if(value.lastIndexOf(".")==value.length()-1){
value=value.substring(0,value.length()-1);
}
this.optValue=value;
this.temp=new StringBuffer("");
}
this.optType=bm.getLabel();
isChoiseOptType=true;
} else if (e.getSource().equals(bd)) {
if(!isChoiseOptType){
value=this.textField.getText();
if(value.lastIndexOf(".")==value.length()-1){
value=value.substring(0,value.length()-1);
}
this.optValue=value;
this.temp=new StringBuffer("");
}
this.optType=bd.getLabel();
isChoiseOptType=true;
}else if (e.getSource().equals(be)) {
if(!this.optType.equals("")){
BigDecimal opt1=new BigDecimal(this.optValue);
value=this.textField.getText();
if(value.lastIndexOf(".")==value.length()-1){
value=value.substring(0,value.length()-1);
}
BigDecimal opt2=new BigDecimal(value);
BigDecimal result=new BigDecimal(0);
if(this.optType.equals("+")){
result=opt1.add(opt2);
}else if(this.optType.equals("-")){
result=opt1.subtract(opt2);
}else if(this.optType.equals("*")){
result=opt1.multiply(opt2);
}else if(this.optType.equals("/")){
result=opt1.divide(opt2);
}else if(this.optType.equals("%")){
result=opt1.remainder(opt2);
}
this.textField.setText(result.toString());
this.temp=new StringBuffer("");
isChoiseOptType=false;
this.optValue="0";
}
} else if (e.getSource().equals(bc)) {
this.temp=new StringBuffer();
this.textField.setText("0");
} else if (e.getSource().equals(bt)) {
value=this.textField.getText();
value=value.substring(0,value.length()-1);
if(value.indexOf("-")=0 value.length()=1){
value="0";
this.temp=new StringBuffer("");
}else{
this.temp=new StringBuffer(value);
}
this.textField.setText(value);
}else if (e.getSource().equals(bh)) {
value=this.textField.getText();
if(value.indexOf("-")==0){
value=value.substring(1,value.length());
}else{
value="-"+value;
}
this.temp=new StringBuffer(value);
this.textField.setText(value);
} else if (e.getSource().equals(bf)) {
this.optValue=this.textField.getText();
if(value.lastIndexOf(".")==value.length()-1){
this.optValue=this.optValue.substring(0,this.optValue.length()-1);
}
Integer opt1=new Integer(this.optValue);
if(!opt1.toString().equals("0")){
this.textField.setText(1.0/opt1.intValue()+"");
System.out.println(1/opt1.intValue()+"");
}else{
this.textField.setText("0");
}
this.temp=new StringBuffer("");
this.optType="";
this.optValue="0";
}
}
}
记住类名是app7.java 包名是main. 如果有不对的地方 到时候在找我 我在线。