}
}
}
// 进行运算的方法
private void operate(char operator)
{
double currentNumber = Double.valueOf(operation.getText()).doubleValue();
if (lastOperator == '?')
storedNumber = currentNumber;
else if (lastOperator == '+')
storedNumber += currentNumber;
else if (lastOperator == '-')
storedNumber -= currentNumber;
else if (lastOperator == '*')
storedNumber *= currentNumber;
else if (lastOperator == '/')
storedNumber /= currentNumber;
else if (lastOperator == 'p')
storedNumber *= -1;
else if (lastOperator == 's')
storedNumber = Math.sqrt(currentNumber);
else if (lastOperator == '='equaled)
storedNumber = currentNumber;
operation.setText("" + storedNumber);
operated = true;
lastOperator = operator;
}
// 快捷使用GridBagLayout的方法
private void addComponent(GridBagLayout layout, Component component, int row, int col, int width, int height)
{
GridBagConstraints constraints = new GridBagConstraints();
constraints.fill = GridBagConstraints.BOTH;
constraints.insets = new Insets(10, 2, 10, 2);
constraints.weightx = 100;
constraints.weighty = 100;
constraints.gridx = col;
constraints.gridy = row;
constraints.gridwidth = width;
constraints.gridheight = height;
layout.setConstraints(component, constraints);
if (component instanceof JButton)
((JButton)component).addActionListener(this);
getContentPane().add(component);
}
// 主方法初始化并显示窗口
public static void main(String[] args)
{
Calculator calc = new Calculator();
calc.setSize(290, 400);
calc.setVisible(true);
}
}
用java编写程序 课题:算术运算测试你好 , 按照你的要求代码如下,可以直接运行
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class test {
public static void main(String[] args) {
int count = 0;
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
for (int i = 0; i10; i++) {
int a = (int) (Math.random() * 100);
int b = (int) (Math.random() * 100);
int c = (int) (Math.random() * 2);
int r = 0;
switch (c) {
case 0:
if (a + b = 100) {
a = 100 - b;
}
System.out.print(a + " + " + b + " = ");
r = a + b;
break;
case 1:
if (ab) {
int t = a;
a = b;
b = t;
}
System.out.print(a + " - " + b + " = ");
r = a - b;
}
int input;
try {
input = Integer.valueOf(in.readLine());
} catch (Exception e1) {
System.out.println("答案错误\n");
continue;
}
if (input == r) {
count++;
System.out.println("答案正确\n");
} else {
System.out.println("答案错误\n");
}
}
if (null != in) {
try {
in.close();
} catch (IOException e) {
e.printStackTrace();
}
}
System.out.println("答对数量:" + count + "\n得分: " + count * 10);
}
}
一道JAVA作业题(小学生加减法测验)package my.test.main;
import java.util.Scanner;
public class Exam {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner in = new Scanner(System.in);
int score = 0;
// 十次循环测试加减法的JAVA代码,出题
for (int i = 0; i10; i++) {
// 通过随机产生1和2,来决定这题是加法还是减法
int operate = (int) (Math.random() * 2 + 1);
int result = 0;// 每题测试加减法的JAVA代码的结果
if (operate == 1) {// 1做加法
int num1, num2;
// 产生连个相加不和不超出50测试加减法的JAVA代码的整数
推荐阅读
- 直播伴侣配件使用,直播伴侣需要什么配置
- mysql服务无法启动为什么,mysql服务器无法启动怎么办
- 很多电脑能做些什么,有很多电脑可以做什么赚钱
- linux删掉命令 linux 删除命令详解
- 影视拍摄用什么场景好看,影视拍摄流程
- 电脑扩内存有什么影响吗,电脑扩充内存可以改善什么
- 日本美女直播平台,日本美女直播平台
- c语言无参函数max C语言无参函数比大小
- sap模块实习生是什么,sap各模块