java运算减法代码 java加减运算代码

希望做一个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 (ab) {// 排除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 javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class Calculator extends JFrame implements ActionListener
{
private boolean dotExist, operated, equaled; // 帮助运算的布尔变量
private double storedNumber; // 目前的结果
private char lastOperator; // 表示上一运算符
private JTextField operation; // 结果栏
private JButton dot, plus, minus, multi, div, sqrt, equal, changePN, clear; // 运算符
private JButton[] numbers; // 数字
// 构造者
public Calculator()
{
setTitle("Calculator");
// 初始化变量
dotExist = false; // 表示当前的数是否有小数点
operated = false; // 表示任意运算符是否被按下
equaled = false; // 表示等号是否被按下
storedNumber = 0;
lastOperator = '?';
// 初始化窗口变量
operation = new JTextField("0");
operation.setEditable(false);
numbers = new JButton[10];
for (int i = 0; i10; i++)
numbers[i] = new JButton("" + i);
dot = new JButton(".");
plus = new JButton("+");
minus = new JButton("-");
multi = new JButton("*");
div = new JButton("/");
sqrt = new JButton("√");
equal = new JButton("=");
changePN = new JButton("±");
clear = new JButton("AC");
// 将窗口物体放入窗口
GridBagLayout layout = new GridBagLayout();
getContentPane().setLayout(layout);
addComponent(layout, operation, 0, 0, 4, 1);
addComponent(layout, numbers[1], 1, 0, 1, 1);
addComponent(layout, numbers[2], 1, 1, 1, 1);
addComponent(layout, numbers[3], 1, 2, 1, 1);
addComponent(layout, numbers[4], 2, 0, 1, 1);
addComponent(layout, numbers[5], 2, 1, 1, 1);
addComponent(layout, numbers[6], 2, 2, 1, 1);
addComponent(layout, numbers[7], 3, 0, 1, 1);
addComponent(layout, numbers[8], 3, 1, 1, 1);
addComponent(layout, numbers[9], 3, 2, 1, 1);
addComponent(layout, dot, 4, 0, 1, 1);
addComponent(layout, numbers[0], 4, 1, 1, 1);
addComponent(layout, sqrt, 4, 2, 1, 1);
addComponent(layout, plus, 1, 3, 1, 1);
addComponent(layout, minus, 2, 3, 1, 1);
addComponent(layout, multi, 3, 3, 1, 1);
addComponent(layout, div, 4, 3, 1, 1);
addComponent(layout, equal, 5, 0, 2, 1);
addComponent(layout, changePN, 5, 2, 1, 1);
addComponent(layout, clear, 5, 3, 1, 1);

推荐阅读