主要涉及的知识点: 类的写法, 以及方法的调用 .建议多做练习. 如果有看不懂的地方. 可以继续追问,一起讨论.
叙永网站建设公司成都创新互联公司,叙永网站设计制作,有大型网站制作公司丰富经验。已为叙永数千家提供企业网站建设服务。企业网站搭建\成都外贸网站制作要多少钱,请找那个售后服务好的叙永做网站的公司定做!
参考代码如下
//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());
}
}
import javax.swing.SwingUtilities;
import javax.swing.JPanel;
import javax.swing.JFrame;
import javax.swing.JTextField;
import java.awt.Rectangle;
import javax.swing.JButton;
public class Calculator extends JFrame {
private static final long serialVersionUID = 1L;
private JPanel jContentPane = null;
private JTextField jTextField = null;
private JButton jButton = null;
private JButton jButton1 = null;
private JButton jButton2 = null;
private JButton jButton3 = null;
private JButton jButton4 = null;
private JButton jButton5 = null;
private JButton jButton6 = null;
private JButton jButton7 = null;
private JButton jButton8 = null;
private JButton jButton9 = null;
private JButton jButton10 = null;
private JButton jButton11 = null;
private JButton jButton12 = null;
private JButton jButton13 = null;
private JButton jButton14 = null;
private double first = 0;
private double second = 0;
private JButton jButton15 = null;
private JButton jButton16 = null;
private String way = "";
private double result = 0;
private char[] ch = new char[12];
private int i = 0;
/**
* This method initializes jTextField
*
* @return javax.swing.JTextField
*/
private JTextField getJTextField() {
if (jTextField == null) {
jTextField = new JTextField("0");
jTextField.setBounds(new Rectangle(60, 15, 234, 31));
jTextField.setHorizontalAlignment(JTextField.RIGHT);
}
return jTextField;
}
/**
* This method initializes jButton
*
* @return javax.swing.JButton
*/
private JButton getJButton() {
if (jButton == null) {
jButton = new JButton();
jButton.setBounds(new Rectangle(62, 74, 44, 37));
jButton.setText("1");
jButton.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent e) {
ch[i] = '1';
jTextField.setText(new String(ch).trim());
i++;
}
});
}
return jButton;
}
/**
* This method initializes jButton1
*
* @return javax.swing.JButton
*/
private JButton getJButton1() {
if (jButton1 == null) {
jButton1 = new JButton();
jButton1.setBounds(new Rectangle(119, 74, 44, 37));
jButton1.setText("2");
jButton1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent e) {
ch[i] = '2';
jTextField.setText(new String(ch).trim());
i++;
}
});
}
return jButton1;
}
/**
* This method initializes jButton2
*
* @return javax.swing.JButton
*/
private JButton getJButton2() {
if (jButton2 == null) {
jButton2 = new JButton();
jButton2.setBounds(new Rectangle(176, 74, 44, 37));
jButton2.setText("3");
jButton2.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent e) {
ch[i] = '3';
jTextField.setText(new String(ch).trim());
i++;
}
});
}
return jButton2;
}
/**
* This method initializes jButton3
*
* @return javax.swing.JButton
*/
private JButton getJButton3() {
if (jButton3 == null) {
jButton3 = new JButton();
jButton3.setBounds(new Rectangle(62, 129, 44, 37));
jButton3.setText("4");
jButton3.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent e) {
ch[i] = '4';
jTextField.setText(new String(ch).trim());
i++;
}
});
}
return jButton3;
}
/**
* This method initializes jButton4
*
* @return javax.swing.JButton
*/
private JButton getJButton4() {
if (jButton4 == null) {
jButton4 = new JButton();
jButton4.setBounds(new Rectangle(119, 129, 44, 37));
jButton4.setText("5");
jButton4.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent e) {
ch[i] = '5';
jTextField.setText(new String(ch).trim());
i++;
}
});
}
return jButton4;
}
/**
* This method initializes jButton5
*
* @return javax.swing.JButton
*/
private JButton getJButton5() {
if (jButton5 == null) {
jButton5 = new JButton();
jButton5.setBounds(new Rectangle(176, 129, 44, 37));
jButton5.setText("6");
jButton5.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent e) {
ch[i] = '6';
jTextField.setText(new String(ch).trim());
i++;
}
});
}
return jButton5;
}
/**
* This method initializes jButton6
*
* @return javax.swing.JButton
*/
private JButton getJButton6() {
if (jButton6 == null) {
jButton6 = new JButton();
jButton6.setBounds(new Rectangle(62, 184, 44, 37));
jButton6.setText("7");
jButton6.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent e) {
ch[i] = '7';
jTextField.setText(new String(ch).trim());
i++;
}
});
}
return jButton6;
}
/**
* This method initializes jButton7
*
* @return javax.swing.JButton
*/
private JButton getJButton7() {
if (jButton7 == null) {
jButton7 = new JButton();
jButton7.setBounds(new Rectangle(119, 184, 44, 37));
jButton7.setText("8");
jButton7.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent e) {
ch[i] = '8';
jTextField.setText(new String(ch).trim());
i++;
}
});
}
return jButton7;
}
/**
* This method initializes jButton8
*
* @return javax.swing.JButton
*/
private JButton getJButton8() {
if (jButton8 == null) {
jButton8 = new JButton();
jButton8.setBounds(new Rectangle(176, 184, 44, 37));
jButton8.setText("9");
jButton8.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent e) {
ch[i] = '9';
jTextField.setText(new String(ch).trim());
i++;
}
});
}
return jButton8;
}
/**
* This method initializes jButton9
*
* @return javax.swing.JButton
*/
private JButton getJButton9() {
if (jButton9 == null) {
jButton9 = new JButton();
jButton9.setBounds(new Rectangle(233, 74, 44, 37));
jButton9.setText("+");
jButton9.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent e) {
first = Double.parseDouble(jTextField.getText());
way = "+";
ch = new char[12];
i = 0;
jButton16.setEnabled(true);
}
});
}
return jButton9;
}
/**
* This method initializes jButton10
*
* @return javax.swing.JButton
*/
private JButton getJButton10() {
if (jButton10 == null) {
jButton10 = new JButton();
jButton10.setBounds(new Rectangle(290, 74, 44, 37));
jButton10.setText("-");
jButton10.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent e) {
first = Double.parseDouble(jTextField.getText());
way = "-";
ch = new char[12];
i = 0;
jButton16.setEnabled(true);
}
});
}
return jButton10;
}
/**
* This method initializes jButton11
*
* @return javax.swing.JButton
*/
private JButton getJButton11() {
if (jButton11 == null) {
jButton11 = new JButton();
jButton11.setBounds(new Rectangle(233, 129, 44, 37));
jButton11.setText("*");
jButton11.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent e) {
first = Double.parseDouble(jTextField.getText());
way = "*";
ch = new char[12];
i = 0;
jButton16.setEnabled(true);
}
});
}
return jButton11;
}
/**
* This method initializes jButton12
*
* @return javax.swing.JButton
*/
private JButton getJButton12() {
if (jButton12 == null) {
jButton12 = new JButton();
jButton12.setBounds(new Rectangle(290, 129, 44, 37));
jButton12.setText("/");
jButton12.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent e) {
first = Double.parseDouble(jTextField.getText());
way = "/";
ch = new char[12];
i = 0;
jButton16.setEnabled(true);
}
});
}
return jButton12;
}
/**
* This method initializes jButton13
*
* @return javax.swing.JButton
*/
private JButton getJButton13() {
if (jButton13 == null) {
jButton13 = new JButton();
jButton13.setBounds(new Rectangle(233, 184, 44, 37));
jButton13.setText("=");
jButton13.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent e) {
second = Double.parseDouble(jTextField.getText());
if (way.equals("")){
jTextField.setText(""+second);
}
if (way.equals("+")){
result = first + second;
jTextField.setText(""+result);
}
if (way.equals("-")){
result = first - second;
jTextField.setText(""+result);
}
if (way.equals("*")){
result = first * second;
jTextField.setText(""+result);
}
if(way.equals("/")){
if (second != 0){
result = first / second;
jTextField.setText(""+result);
}else{
jTextField.setText("error");
}
}
jButton16.setEnabled(true);
}
});
}
return jButton13;
}
/**
* This method initializes jButton14
*
* @return javax.swing.JButton
*/
private JButton getJButton14() {
if (jButton14 == null) {
jButton14 = new JButton();
jButton14.setBounds(new Rectangle(290, 184, 44, 37));
jButton14.setText("C");
jButton14.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent e) {
jTextField.setText("0");
first = 0;
second = 0;
way = "";
ch = new char[12];
i = 0;
jButton16.setEnabled(true);
}
});
}
return jButton14;
}
/**
* This method initializes jButton15
*
* @return javax.swing.JButton
*/
private JButton getJButton15() {
if (jButton15 == null) {
jButton15 = new JButton();
jButton15.setBounds(new Rectangle(62, 231, 102, 26));
jButton15.setText("0");
jButton15.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent e) {
ch[i] = '0';
jTextField.setText(new String(ch).trim());
i++;
}
});
}
return jButton15;
}
/**
* This method initializes jButton16
*
* @return javax.swing.JButton
*/
private JButton getJButton16() {
if (jButton16 == null) {
jButton16 = new JButton();
jButton16.setBounds(new Rectangle(176, 231, 44, 26));
jButton16.setText(".");
jButton16.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent e) {
if(ch[0] == 0){
ch[i] = '0';
ch[i+1] = '.';
i++;
}else{
ch[i] = '.';
}
jTextField.setText(new String(ch).trim());
i++;
jButton16.setEnabled(false);
}
});
}
return jButton16;
}
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
SwingUtilities.invokeLater(new Runnable() {
public void run() {
Calculator thisClass = new Calculator();
thisClass.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
thisClass.setVisible(true);
}
});
}
/**
* This is the default constructor
*/
public Calculator() {
super();
initialize();
}
/**
* This method initializes this
*
* @return void
*/
private void initialize() {
this.setSize(356, 298);
this.setContentPane(getJContentPane());
this.setTitle("计算器");
this.setResizable(false);
}
/**
* This method initializes jContentPane
*
* @return javax.swing.JPanel
*/
private JPanel getJContentPane() {
if (jContentPane == null) {
jContentPane = new JPanel();
jContentPane.setLayout(null);
jContentPane.add(getJTextField(), null);
jContentPane.add(getJButton(), null);
jContentPane.add(getJButton1(), null);
jContentPane.add(getJButton2(), null);
jContentPane.add(getJButton3(), null);
jContentPane.add(getJButton4(), null);
jContentPane.add(getJButton5(), null);
jContentPane.add(getJButton6(), null);
jContentPane.add(getJButton7(), null);
jContentPane.add(getJButton8(), null);
jContentPane.add(getJButton9(), null);
jContentPane.add(getJButton10(), null);
jContentPane.add(getJButton11(), null);
jContentPane.add(getJButton12(), null);
jContentPane.add(getJButton13(), null);
jContentPane.add(getJButton14(), null);
jContentPane.add(getJButton15(), null);
jContentPane.add(getJButton16(), null);
}
return jContentPane;
}
} // @jve:decl-index=0:visual-constraint="437,27"
哥们!这是用eclipse中的visual editor做的,所以代码比较多,复制粘贴不方便,我可以发你邮箱。你把代码打上去,一样能运行的!!!
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;
}
}
}
售后响应及时
7×24小时客服热线数据备份
更安全、更高效、更稳定价格公道精准
项目经理精准报价不弄虚作假合作无风险
重合同讲信誉,无效全额退款