资讯

精准传达 • 有效沟通

从品牌网站建设到网络营销策划,从策略到执行的一站式服务

java做加减代码 java如何实现加减乘除

用java写个简单的加减计算器

不仅可以加减还可以乘除,代码如下:

专注于为中小企业提供成都网站制作、成都做网站服务,电脑端+手机端+微信端的三站合一,更高效的管理,为中小企业马鞍山免费做网站提供优质的服务。我们立足成都,凝聚了一批互联网行业人才,有力地推动了上千家企业的稳健成长,帮助中小企业通过网站建设实现规模扩充和转变。

package 计算器;

import java.awt.BorderLayout;

import java.awt.Container;

import java.awt.GridLayout;

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

import java.util.regex.Matcher;

import java.util.regex.Pattern;

import javax.swing.JButton;

import javax.swing.JFrame;

import javax.swing.JOptionPane;

import javax.swing.JPanel;

import javax.swing.JTextField;

public class Calculator extends JFrame implements ActionListener {

boolean flag = false;

JTextField jtf = new JTextField();

public static void main(String[] args) {

new Calculator();

}

public Calculator() {

setSize(300, 300);

setTitle("计算器");

Container c = getContentPane();

jtf.setHorizontalAlignment(JTextField.RIGHT);

JButton jb = new JButton("=");

jb.addActionListener(this);

JPanel jp = new JPanel();

c.add(jtf, BorderLayout.NORTH);

c.add(jp, BorderLayout.CENTER);

c.add(jb, BorderLayout.SOUTH);

jp.setLayout(new GridLayout(4, 4));

addButton(jp);

setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

setResizable(false);

setVisible(true);

}

public void addButton(JPanel jp) {

JButton b = null;

b = new JButton("1");

b.addActionListener(this);

jp.add(b);

b = new JButton("2");

b.addActionListener(this);

jp.add(b);

b = new JButton("3");

b.addActionListener(this);

jp.add(b);

b = new JButton("+");

b.addActionListener(this);

jp.add(b);

b = new JButton("4");

b.addActionListener(this);

jp.add(b);

b = new JButton("5");

b.addActionListener(this);

jp.add(b);

b = new JButton("6");

b.addActionListener(this);

jp.add(b);

b = new JButton("-");

b.addActionListener(this);

jp.add(b);

b = new JButton("7");

b.addActionListener(this);

jp.add(b);

b = new JButton("8");

b.addActionListener(this);

jp.add(b);

b = new JButton("9");

b.addActionListener(this);

jp.add(b);

b = new JButton("*");

b.addActionListener(this);

jp.add(b);

b = new JButton("0");

b.addActionListener(this);

jp.add(b);

b = new JButton(".");

b.addActionListener(this);

jp.add(b);

b = new JButton("C");

b.addActionListener(this);

jp.add(b);

b = new JButton("/");

b.addActionListener(this);

jp.add(b);

}

public void actionPerformed(ActionEvent e) {

if (e.getActionCommand() == "=") {

if (jtf.getText().matches("\\d+\\.?\\d*[\\+\\-\\*\\/]\\d+\\.?\\d*")) {

String s = jtf.getText();

Pattern p = Pattern.compile("\\d+\\.?\\d*");

Matcher m = p.matcher(s);

m.find();

double firstNum = Double.parseDouble(m.group());

m.find();

double secondNum = Double.parseDouble(m.group());

if (jtf.getText().matches("\\d+\\.?\\d*\\+\\d+\\.?\\d*")) {

jtf.setText("" + (firstNum + secondNum));

} else if (jtf.getText().matches("\\d+\\.?\\d*\\-\\d+\\.?\\d*")) {

jtf.setText("" + (firstNum - secondNum));

} else if (jtf.getText().matches("\\d+\\.?\\d*\\*\\d+\\.?\\d*")) {

jtf.setText("" + (firstNum * secondNum));

} else if (jtf.getText().matches("\\d+\\.?\\d*\\/\\d+\\.?\\d*")) {

jtf.setText("" + (firstNum / secondNum));

}

} else {

JOptionPane.showMessageDialog(null, "请输入正确表达式!");

}

flag = true;

} else if (e.getActionCommand() == "C") {

jtf.setText("");

} else {

if (flag) {

if (e.getActionCommand() != "+" e.getActionCommand() != "-"

e.getActionCommand() != "*"

e.getActionCommand() != "/") {

//System.out.println(e.getActionCommand() != "+");

jtf.setText("");

}

flag = false;

}

jtf.setText(jtf.getText() + e.getActionCommand());

}

}

}

希望做一个JAVA一百以内的加减法源代码

import java.util.Scanner;

public class JianJian {

public static void main(String[] args) {

System.out.println("一百以内的加减法");

System.out.println("输入-1,退出系统");

Scanner sc = new Scanner(System.in);

int score = 0;

int nums = 0;

while (true) {

int x = (int) (Math.random() * 2);//随机一个0,1的数字,当数字是1的时候,输出加法,当数字是0的时候,输出减法

int a = (int) (Math.random() * 100);

int b = (int) (Math.random() * 100);

if (x == 1) {

System.out.print(a + "+" + b + "=");

int c = sc.nextInt();

if (c == -1) {

break;

}

nums++;

if (c == (a + b)) {

score = score + 10;

}

} else {

if (a  b) {// 排除a-b0的情况.保证结果都是大于0的

b = a + b;

a = b - a;

b = b - a;

}

System.out.print(a + "-" + b + "=");

int c = sc.nextInt();

if (c == -1) {

break;

}

nums++;

if (c == (a - b)) {

score = score + 10;

}

}

}

System.out.println("做了" + nums + "道题目,得分: " + score);

}

}

输出

一百以内的加减法

输入-1,退出系统

87-18=59

23-6=17

72-30=42

97+14=-1//-1表示退出系统,所以不算做一道题

做了3道题目,得分: 20

关于java加减除的代码问题

import java.util.Scanner;

public class P {

public static void main(String[] args) {

Scanner sc=new Scanner(System.in);

P prm=new P();

int a,b;

System.out.println("请输入两个整数(以空格分隔):");

a=sc.nextInt();

b=sc.nextInt();

System.out.println("两数相加后等于:"+prm.add(a,b));

System.out.println("两数相减后等于:"+prm.sub(a,b));

System.out.println("两数相除后等于:"+prm.divide(b,a));

sc.close();

}

int add(int a,int b) {

return a+b;

}

int sub(int a,int b) {

if(ab)

return a-b;

else

return b-a;

}

int divide(int a,int b) {

if(0==b)

System.out.println("错误");

if(0==a)

System.out.println("除数不能为0");

return b/a;

}

}


分享题目:java做加减代码 java如何实现加减乘除
分享网址:http://cdkjz.cn/article/ddodcdc.html
多年建站经验

多一份参考,总有益处

联系快上网,免费获得专属《策划方案》及报价

咨询相关问题或预约面谈,可以通过以下方式与我们联系

业务热线:400-028-6601 / 大客户专线   成都:13518219792   座机:028-86922220