java 实现一个简单的计算器,能做100以内的加减乘除

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了java 实现一个简单的计算器,能做100以内的加减乘除相关的知识,希望对你有一定的参考价值。

网上找的,如果你是要求的这样的,自己写也要好一会:

import java.awt.*;
import java.awt.event.*;

public class Calculator

private Frame f;
private TextField tf = new TextField(30);
private long result;
private boolean append = false;
private Button[] btn = new Button[15];
private char operator = \'=\';

public static void main(String[] args)
new Calculator().go();


public Calculator()
initComponent();


private void initComponent()
f = new Frame("Java Calculator");
f.setLayout(new BorderLayout()); // The frame uses BorderLayout
f.addWindowListener(new WindowAdapter()
public void windowClosing(WindowEvent evt)
System.exit(0);

);

Panel centerPanel = new Panel();
centerPanel.setLayout(new GridLayout(5, 3)); // The panel uses GridLayout

NumberListener nl = new NumberListener();
OperatorListener ol = new OperatorListener();

btn[10] = new Button("+");
btn[11] = new Button("-");
btn[12] = new Button("*");
btn[13] = new Button("/");
btn[14] = new Button("=");

for (int i = 0; i <= 9; i++)
btn[i] = new Button(String.valueOf(i));
centerPanel.add(btn[i]);
btn[i].addActionListener(nl);
if (i % 2 == 1)
centerPanel.add(btn[(i + 19) / 2]);
btn[(i + 19) / 2].addActionListener(ol);


f.add(centerPanel, BorderLayout.CENTER);

Panel northPanel = new Panel();

tf.setEditable(false);
northPanel.add(tf);
f.add(northPanel, BorderLayout.NORTH);


public void go()
f.pack();
f.setVisible(true);


/**
* 采用成员内部类方式,实现监听器接口,方便访问主类内类内部成员。 此类负责数字按钮Action事件监听和处理
*/
class NumberListener implements ActionListener
public void actionPerformed(ActionEvent e)
if (!append)
tf.setText("");
append = true;

String s = tf.getText();
s += e.getActionCommand();
tf.setText(s);
if (!btn[10].isEnabled())
for (int i = 10; i <= 14; i++)
btn[i].setEnabled(true);




/**
* 成员内部类,负责操作符按钮的事件处理
*/
class OperatorListener implements ActionListener
public void actionPerformed(ActionEvent e)
if (!append)
return;
for (int i = 10; i <= 14; i++)
btn[i].setEnabled(false);
String s = tf.getText();
long num = Long.parseLong(s);// get the number of textfield
append = false; // set append
switch (operator)
case \'+\':
result += num;
break;
case \'-\':
result -= num;
break;
case \'*\':
result *= num;
break;
case \'/\':
if (num == 0)
result = 0;
else
result /= num;
break;

case \'=\':
result = num;
break;

tf.setText(String.valueOf(result));
// set the value of result to textfield
String op = e.getActionCommand();
operator = op.charAt(0); // set operator


参考技术A 太简单了~~~ 参考技术B 太简单了。

Java实现随机出题,10道10以内加减法计算

package com.swift;

import java.awt.Toolkit;
import java.util.Scanner;

public class PlusQuiz {

    public static void main(String[] args) {
        int i=0;
        int number1=0,number2=0;
        for(;;) {
            number1=(int) (Math.random()*10);
            number2=(int) (Math.random()*10);
            //加法部分
            System.out.println("What is "+number1+"+"+number2+"?");
            Scanner scan=new Scanner(System.in);
            int answer=scan.nextInt();
            if(answer==number1+number2) {
                System.out.println("√");
                Toolkit.getDefaultToolkit().beep();
                //System.out.println(‘\007‘);
                
            }else {
                System.out.println("×");
                System.out.println(number1+"+"+number2+" should be "+(number1+number2));
            }
            //减法部分
            if(number1<number2) {
                int temp;
                temp=number1;
                number1=number2;
                number2=temp;
            }
            System.out.println("What is "+number1+"-"+number2+"?");
            int answer2=scan.nextInt();
            if(answer2==number1-number2) {
                System.out.println("√");
                for(int time=0;time<6;time++)
                Toolkit.getDefaultToolkit().beep();
            }else {
                System.out.println("×");
                System.out.println(number1+"-"+number2+" should be "+(number1-number2));
            }
            //判断加减法完成次数
            i++;
            if(i==5) {
                break;
            }
        }
    }

}

 

以上是关于java 实现一个简单的计算器,能做100以内的加减乘除的主要内容,如果未能解决你的问题,请参考以下文章

团队项目计划会议之个人

for计算100以内的奇数和

bash简单for循环2

Java简单算法--求100以内素数

用for循环计算100以内的奇数之和

简单程序实现100以内加减乘除