javamycalc的简单介绍

博主:adminadmin 2022-12-10 06:48:05 73

本篇文章给大家谈谈javamycalc,以及对应的知识点,希望对各位有所帮助,不要忘了收藏本站喔。

本文目录一览:

用Java做一个简单的计算器

窗体

package Calc;

import java.awt.*;

import java.awt.event.*;

import javax.swing.*;

/**

*

* 计算器程序

*

*/

public class CalcFrame extends JFrame {

JButton[] buttons = new JButton[16];// 16个按钮

StringBuffer sb = null;//

JTextField text1 = null;// 计算器结果显示框

String no1 = null;

String sign = null;// 表示+-*/符号

String[] s = { "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "+", "-",

  "*", "/", "=", "C" };// 面板上的字符

public CalcFrame() {

 setTitle("计算器");

 setResizable(false);

 setBounds(200, 200, 300, 350);

 setLayout(null);

 sb = new StringBuffer();

 text1 = new JTextField();

 text1.setBounds(10, 10, 250, 30);// 设置位置

 text1.setFont(new Font("Arial", Font.PLAIN, 20));// 设置字体

 text1.setForeground(Color.RED);// 设置颜色

 add(text1);

 for (int i = 0; i s.length; i++) {

  buttons[i] = new JButton(s[i]);

  buttons[i].setFont(new Font("Serif", Font.BOLD, 18));

  add(buttons[i]);

 }// 生成面板上的按钮.

 buttons[0].setBounds(10, 50, 50, 40);

 buttons[0].addActionListener(new ActionListener() {

  public void actionPerformed(ActionEvent e) {

   sb.append(0);

   text1.setText(sb.toString());

  }

 });

 buttons[1].setBounds(70, 50, 50, 40);

 buttons[1].addActionListener(new ActionListener() {

  public void actionPerformed(ActionEvent e) {

   sb.append(1);

   text1.setText(sb.toString());

  }

 });

 buttons[2].setBounds(130, 50, 50, 40);

 buttons[2].addActionListener(new ActionListener() {

  public void actionPerformed(ActionEvent e) {

   sb.append(2);

   text1.setText(sb.toString());

  }

 });

 buttons[3].setBounds(190, 50, 50, 40);

 buttons[3].addActionListener(new ActionListener() {

  public void actionPerformed(ActionEvent e) {

   sb.append(3);

   text1.setText(sb.toString());

  }

 });

 buttons[4].setBounds(10, 100, 50, 40);

 buttons[4].addActionListener(new ActionListener() {

  public void actionPerformed(ActionEvent e) {

   sb.append(4);

   text1.setText(sb.toString());

  }

 });

 buttons[5].setBounds(70, 100, 50, 40);

 buttons[5].addActionListener(new ActionListener() {

  public void actionPerformed(ActionEvent e) {

   sb.append(5);

   text1.setText(sb.toString());

  }

 });

 buttons[6].setBounds(130, 100, 50, 40);

 buttons[6].addActionListener(new ActionListener() {

  public void actionPerformed(ActionEvent e) {

   sb.append(6);

   text1.setText(sb.toString());

  }

 });

 buttons[7].setBounds(190, 100, 50, 40);

 buttons[7].addActionListener(new ActionListener() {

  public void actionPerformed(ActionEvent e) {

   sb.append(7);

   text1.setText(sb.toString());

  }

 });

 buttons[8].setBounds(10, 150, 50, 40);

 buttons[8].addActionListener(new ActionListener() {

  public void actionPerformed(ActionEvent e) {

   sb.append(8);

   text1.setText(sb.toString());

  }

 });

 buttons[9].setBounds(70, 150, 50, 40);

 buttons[9].addActionListener(new ActionListener() {

  public void actionPerformed(ActionEvent e) {

   sb.append(9);

   text1.setText(sb.toString());

  }

 });

 buttons[10].setBounds(130, 150, 50, 40);

 buttons[10].addActionListener(new ActionListener() {

  public void actionPerformed(ActionEvent e) {

   sign = "+";

   no1 = text1.getText();

   sb.delete(0, sb.capacity());

  }

 });

 buttons[11].setBounds(190, 150, 50, 40);

 buttons[11].addActionListener(new ActionListener() {

  public void actionPerformed(ActionEvent e) {

   sign = "-";

   no1 = text1.getText();

   sb.delete(0, sb.capacity());

  }

 });

 buttons[12].setBounds(10, 200, 50, 40);

 buttons[12].addActionListener(new ActionListener() {

  public void actionPerformed(ActionEvent e) {

   sign = "*";

   no1 = text1.getText();

   sb.delete(0, sb.capacity());

  }

 });

 buttons[13].setBounds(70, 200, 50, 40);

 buttons[13].addActionListener(new ActionListener() {

  public void actionPerformed(ActionEvent e) {

   sign = "/";

   no1 = text1.getText();

   sb.delete(0, sb.capacity());

  }

 });

 buttons[14].setForeground(Color.RED);

 buttons[14].setBounds(130, 200, 50, 40);

 buttons[14].addActionListener(new ActionListener() {

  public void actionPerformed(ActionEvent e) {

   int sum = 0;//最终结果

   if (sign.equals("+")) {

    int no2 = Integer.parseInt(no1);//取得前一个数

    int no3 = Integer.parseInt(text1.getText());//取得现在的数

    sum = no2 + no3;//累加

    text1.setText(String.valueOf(sum));

   }

   if (sign.equals("-")) {

    int no2 = Integer.parseInt(no1);

    int no3 = Integer.parseInt(text1.getText());

    sum = no2 - no3;

    text1.setText(String.valueOf(sum));

   }

   if (sign.equals("*")) {

    int no2 = Integer.parseInt(no1);

    int no3 = Integer.parseInt(text1.getText());

    sum = no2 * no3;

    text1.setText(String.valueOf(sum));

   }

   if (sign.equals("/")) {

    int no2 = Integer.parseInt(no1);

    int no3 = Integer.parseInt(text1.getText());

    sum = no2 / no3;

    text1.setText(String.valueOf(sum));

   }

  }

 });

 buttons[15].setBounds(190, 200, 50, 40);

 buttons[15].addActionListener(new ActionListener() {

  public void actionPerformed(ActionEvent e) {

   sb.delete(0, sb.capacity());//清除sb的内容

   text1.setText("");//结果框设置为空

  }

 });

 setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//响应关闭窗口

 setVisible(true);//显示窗口

}

}

2.主程序类

package Calc;

public class CalcTest {

public static void main(String[] args) {

 CalcFrame f = new CalcFrame();

}

}

学习Java就到IT学习联盟

java程序题目:输入一个十进制整数,分别以二进制、八进制、十六进制形式输出该整数,图形界面文本框竖排

public class Calc extends Frame implements ActionListener {

TextField text1, text3, text4, text5;

Button btn;

public Calc() {

setLayout(new GridLayout(7, 3));

// 第一行

text1 = new TextField(10);

add(text1);

btn = new Button("do");

btn.addActionListener(this);

add(btn);

text3 = new TextField(30);

add(text3);

text4 = new TextField(30);

add(text4);

text5 = new TextField(30);

add(text5);

}

public void actionPerformed(ActionEvent e) {

if (e.getSource().equals(btn)) {

text3.setText("二进制数是:" + String.valueOf(Integer.toBinaryString(Integer.valueOf(text1.getText()))));

text4.setText("八进制数是:" + String.valueOf(Integer.toOctalString(Integer.valueOf(text1.getText()))));

text5.setText("十六进制数是:" + String.valueOf(Integer.toHexString(Integer.valueOf(text1.getText()))).toUpperCase());

}

}

public static void main(String[] agrs) {

Calc my = new Calc();

my.setSize(250, 150);

my.setVisible(true);

}

}

java calc是什么意思?

void calc(int a, int b, int c){

a*=2;

b*=3;

c*=4;

}

这是自己定义的一个方法(method)。执行一组操作。把参数中的 a、b、c 分别乘以了 2、3、4。

然后 calc(a, b, c); 这一句就是对这个方法的调用。可以理解为把那组操作放在这个位置执行。

这个程序输出的话应该是:

10 13 24

10 13 24

之所以第二遍没有乘上相应的数,是因为方法中的整数形参是传值的。

void calc(int a, int b, int c){

a*=2;

b*=3;

c*=4;

}

这个方法里的 a、b、c 区别于主方法里的 a、b、c,他们是不一样的。这个方法并不会改变主方法里的 a、b、c。

javamycalc的介绍就聊到这里吧,感谢你花时间阅读本站内容,更多关于、javamycalc的信息别忘了在本站进行查找喔。

The End

发布于:2022-12-10,除非注明,否则均为首码项目网原创文章,转载请注明出处。