java算术运算测试代码 java算式

JAVA 算术运算测试import java.util.Random;
import java.util.Scanner;
public class TestRandom {
public static void main(String[] args) {
Random rand = new Random();
Scanner input = new Scanner(System.in);
while(true){
int a = rand.nextInt(100);
int b = rand.nextInt(100);
int result = a b;
System.out.println(a " " b "=?");
intyourGuess=input.nextInt();
if(yourGuess == result){
System.out.println("you are right");
}else {
System.out.println("you are wrong");
}
}
}
}
java编写一个小软件用于测试小学生10以内四则运算的口算能力我可以说一下思路:
取到所有下拉框的值,还有这个输入框的值 , 比如第一个是: num1 第二个是运算符:* 第三个是:num2 第四个是结果: result
点击提交,给一个点击事件 , 把取到的值进行运算var cal= Integer.parseInt(num1)* Integer.parseInt(num2),cla是自己定义的,然后判断程序计算的结果是否和输入的结果相等,再给出相应的提示
用Java设计一个小学算术运算测试软件java算术运算测试代码你的想法是好的java算术运算测试代码,但是我仔细构思了一遍java算术运算测试代码,太难了 。
首先难点就是怎么生成题目 。也是最难得 , 因为有太多不确定,弄不好就会出现负数结果的题目 。
然后是题目的解答,这个难点就不说了,这个我在几年前就写好了 。一直没用上,现在却用上了 。
还有,你要窗体 , 可是我不会用swing
还有你要的四则混合运算是多长的,我总不能写一个有1000多个数字写成的式子吧 。
题目中是否涉及到小数
下面是我的计算代码java算术运算测试代码:
package compute;
public class CaculateString {
private String str, s;
private int p1, p2;
public CaculateString(String str) {
this.str = str;
this.clear();
this.check();
}
public String caculate() {
while (isEnd(str)) {
p2 = str.indexOf(")");
if (p2 != -1) {
s = str.substring(0, p2);
p1 = s.lastIndexOf("(");
s = str.substring(p11, p2);
if (!s.equals("")) {
s = this.caculateNumber(s);
if (p2 == (str.length() - 1))
str = str.substring(0, p1)s;
else
str = str.substring(0, p1)sstr.substring(p21);
} else {
str = str.substring(0, p1)str.substring(p21);
}
} else
str = this.caculateNumber(str);
}
return str;
}
private String caculateNumber(String ss) {
int p3 = 0, p4 = 0, p5 = 0;
char b = ' ', c = ' ';
String s1 = null, s2 = null;
double d1 = 0.0, d2 = 0.0, d3 = 0.0;
if (ss.charAt(0) == ' ') {
ss = ss.substring(1);
if (ss.indexOf(" ") == -1ss.indexOf("-") == -1
ss.indexOf("*") == -1ss.indexOf("/") == -1)
return ss;
} else if (ss.charAt(0) == '-') {
if (ss.indexOf(" ", 1) == -1ss.indexOf("-", 1) == -1
ss.indexOf("*", 1) == -1ss.indexOf("/", 1) == -1)
return ss;
}
while (isEnd2(ss)) {
p3 = 0;
p4 = 0;
p5 = 0;
if (ss.indexOf("*") == -1ss.indexOf("/") == -1) {
for (int i = 0; iss.length(); i) {
b = ss.charAt(i);
if (b == ' ' || b == '-') {
if (p3 == 0i != 0) {
p3 = i;
c = b;
} else if (p3 != 0i != (p31)) {
p4 = i;
break;
}
}
}
if (p4 == 0) {
s1 = ss.substring(0, p3);
s2 = ss.substring(p31);
} else {
s1 = ss.substring(0, p3);
s2 = ss.substring(p31, p4);
}
d1 = Double.parseDouble(s1);
d2 = Double.parseDouble(s2);
if (c == ' ')
d3 = d1d2;
else if (c == '-')
d3 = d1 - d2;
if (p4 == 0)
ss = new Double(d3).toString();
else
ss = new Double(d3).toString()ss.substring(p4);
} else {
if (ss.indexOf("*") == -1) {
c = '/';
p4 = ss.indexOf("/");
} else if (ss.indexOf("/") == -1) {
c = '*';
p4 = ss.indexOf("*");
} else if (ss.indexOf("*")ss.indexOf("/")) {
c = '/';
p4 = ss.indexOf("/");
} else if (ss.indexOf("*")ss.indexOf("/")) {
c = '*';
p4 = ss.indexOf("*");
}
for (int i = p4 - 1; i = 0; i--) {
b = ss.charAt(i);
if (b == ' ' || b == '-') {
if ((i - 1) != 0ss.charAt(i - 1) != ' '
ss.charAt(i - 1) != '-') {
p3 = i;
break;
} else if ((i - 1) != 0
(ss.charAt(i - 1) == ' ' || ss.charAt(i - 1) == '-')) {
p3 = i - 1;
break;
}
}
}
for (int i = p41; iss.length(); i) {
b = ss.charAt(i);
if ((b == ' ' || b == '-' || b == '*' || b == '/')
(i - 1) != p4) {
p5 = i;
break;
}
}
if (p3 == 0)
s1 = ss.substring(0, p4);
else
s1 = ss.substring(p31, p4);
if (p5 == 0)
s2 = ss.substring(p41);
else
s2 = ss.substring(p41, p5);
d1 = Double.parseDouble(s1);
d2 = Double.parseDouble(s2);
if (c == '*')
d3 = d1 * d2;
else if (c == '/')
d3 = d1 / d2;
if (p3 == 0p5 == 0)
ss = new Double(d3).toString();
else if (p3 == 0p5 != 0)
ss = new Double(d3).toString()ss.substring(p5);
else if (p3 != 0p5 == 0)
ss = ss.substring(0, p31)new Double(d3).toString();
else if (p3 != 0p5 != 0)
ss = ss.substring(0, p31)new Double(d3).toString()
ss.substring(p5);
}
}
return ss;
}
private boolean isEnd(String str) {
if (str.indexOf("(") != -1 || str.indexOf(")") != -1
|| str.indexOf(" ", 1) != -1 || str.indexOf("-", 1) != -1
|| str.indexOf("*", 1) != -1 || str.indexOf("/", 1) != -1)
return true;
return false;
}
private boolean isEnd2(String ss) {
if (ss.indexOf(" ", 1) != -1 || ss.indexOf("-", 1) != -1
|| ss.indexOf("*", 1) != -1 || ss.indexOf("/", 1) != -1)
return true;
return false;
}
private void clear() {
str = str.replaceAll(" ", "");
}
private void check() {
int right = 0, left = 0;
for (int i = 0; istr.length(); i) {
char a = str.charAt(i);
if (a != '0'a != '1'a != '2'a != '3'a != '4'
a != '5'a != '6'a != '7'a != '8'a != '9'
a != ' 'a != '-'a != '*'a != '/'a != '('
a != ')'a != '.') {
System.exit(0);
}
if (a == '(')
left;
else if (a == ')')
right;
}
if (left != right) {
System.exit(0);
}
}
public static void main(String args[]) {
System.out.println(new CaculateString("((1-(0/3))*7) 8").caculate());
}
}
java课程设计“小学算术运算测试程序”花了好几个小时,测试通过,希望楼主多给点分
import java.util.Scanner;
class Question{
private int firstParam;
private int secondParam;
private String operator;
private int answer;
private int score;
public int getFirstParam() {
return firstParam;
}
public void setFirstParam(int firstParam) {
this.firstParam = firstParam;
}
public int getSecondParam() {
return secondParam;
}
public void setSecondParam(int secondParam) {
this.secondParam = secondParam;
}
public String getOperator() {
return operator;
}
public void setOperator(String operator) {
this.operator = operator;
}
public int getAnswer() {
return answer;
}
public void setAnswer(int answer) {
this.answer = answer;
}
public int getScore() {
return score;
}
public void setScore(int score) {
this.score = score;
}
//构造
public Question(int firstParam, int secondParam) {
this.firstParam = firstParam;
this.secondParam = secondParam;
}
}
public class StudentStudy {
private int questionNumber;
private String operator;
private Question[] questions;
private int rightNumber=0;
private int wrongNumber=0;
public String getOperator() {
return operator;
}
public void setOperator(String operator) {
this.operator = operator;
}
public int getQuestionNumber() {
return questionNumber;
}
public void setQuestionNumber(int questionNumber) {
this.questionNumber = questionNumber;
}
public int getRightNumber() {
return rightNumber;
}
public void setRightNumber(int rightNumber) {
this.rightNumber = rightNumber;
}
public int getWrongNumber() {
return wrongNumber;
}
public void setWrongNumber(int wrongNumber) {
this.wrongNumber = wrongNumber;
}
public Question[] getQuestions() {
return questions;
}
public void setQuestions(Question[] questions) {
this.questions = questions;
}
public int getRandom(){
return (int)(Math.random()*10);
}
public int calculateByOperator(String operator,int first,int second){
switch(operator.charAt(0)){
case ' ':
return first second;
case '-':
return first-second;
default:
return 0;
}
}
public void makeQuestion(){
mywhile:
while(questions[questionNumber-1]==null){
Question question=new Question(getRandom(),getRandom());
for(int i=0;questions[i]!=nulliquestionNumber;i){
if((question.getFirstParam()==questions[i].getFirstParam())(question.getSecondParam()==questions[i].getSecondParam())){
continue mywhile;
}
}
for(int i=0;iquestionNumber;i){
if(questions[i]==null){
questions[i]=question;
questions[i].setOperator(operator);
questions[i].setScore(100/questionNumber);
questions[i].setAnswer(calculateByOperator(operator,questions[i].getFirstParam(),questions[i].getSecondParam()));
break;
}
}
}
}
public StudentStudy(int questionNumber){
this.questionNumber=questionNumber;
questions=new Question[this.questionNumber];
}
public static void main(String[] args){
Scanner input=new Scanner(System.in);
System.out.println("现在开始测试,请填写要测试的试题数量(1-100):");
int number=input.nextInt();
StudentStudy ss=new StudentStudy(number);
System.out.println("请选择要测试的试题类型:1加法2减法");
int type=input.nextInt();
switch(type){
case 1:
ss.setOperator(" ");
break;
case 2:
ss.setOperator("-");
break;
default:
System.out.println("输入有误,退出系统!");
System.exit(0);
}
ss.makeQuestion();
for(int i=0;inumber;i){
System.out.println("第" (i 1) "题:" ss.getQuestions()[i].getFirstParam() ss.getQuestions()[i].getOperator() ss.getQuestions()[i].getSecondParam() "=?");
int answer=input.nextInt();
if(ss.getQuestions()[i].getAnswer()==answer){
ss.setRightNumber(ss.getRightNumber() 1);
System.out.println("回答正确!");
}
else{
ss.setWrongNumber(ss.getWrongNumber() 1);
System.out.println("回答错误!");
}
}
System.out.println("您总共答了" ss.getQuestionNumber() "道题,答对" ss.getRightNumber() "道,答错" ss.getWrongNumber() "道,最后总分为:" ss.getRightNumber()*100/ss.getQuestionNumber() "分");
}
}
求简单java写计算器代码加减乘除import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class Calculator extends JFrame implements ActionListener
{
private boolean dotExist, operated, equaled; // 帮助运算java算术运算测试代码的布尔变量
private double storedNumber; // 目前java算术运算测试代码的结果
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; // 表示当前java算术运算测试代码的数是否有小数点
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);
}
// 对按钮进行反应java算术运算测试代码的方法
public void actionPerformed(ActionEvent e)
{
JButton btn = (JButton)e.getSource();
if (btn == clear)
{
operation.setText("0");
dotExist = false;
storedNumber = 0;
lastOperator = '?';
}
else if (btn == equal)
{
operate('=');
equaled = true;
}
else if (btn == plus)
{
operate(' ');
equaled = false;
}
else if (btn == minus)
{
operate('-');
equaled = false;
}
else if (btn == multi)
{
operate('*');
equaled = false;
}
else if (btn == div)
{
operate('/');
equaled = false;
}
else if (btn == changePN)
{
operate('p');
operate('=');
equaled = true;
}
else if (btn == sqrt)
{
operate('s');
operate('=');
equaled = true;
}
else
{
if (equaled)
storedNumber = 0;
for (int i = 0; i10; i)
if (btn == numbers[i])
{
if (operation.getText().equals("0"))
operation.setText(""i);
else if(! operated)
operation.setText(operation.getText()i);
else
{
operation.setText(""i);
operated = false;
}
}
if (btn == dot! dotExist)
{
operation.setText(operation.getText()".");
dotExist = true;
}
}
}
// 进行运算java算术运算测试代码的方法
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算术运算测试代码 java算式】java算术运算测试代码的介绍就聊到这里吧,感谢你花时间阅读本站内容,更多关于java算式、java算术运算测试代码的信息别忘了在本站进行查找喔 。

    推荐阅读