java课程设计“小学算术运算测试程序”

java课程设计“小学算术运算测试程序”,第1张

花了好几个小时,测试通过,希望楼主多给点分

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=0questions[i]!=null&&i<questionNumberi++){

if((question.getFirstParam()==questions[i].getFirstParam())&&(question.getSecondParam()==questions[i].getSecondParam())){

continue mywhile

}

}

for(int i=0i<questionNumberi++){

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=0i<numberi++){

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()+"分")

}

}

你好,按照你的要求代码如下睁隐,可以直接运行

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 = 0i <10i++) {

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 (a <b) {

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)

}

}

package com.demo

import java.math.BigDecimal

import java.util.Random

import java.util.Scanner

/*

 * Java编写一个能够自动生成小朋友算术练习题目的程序。设计一个程序,要求能够随机生成每个题目。

 * 编写一个能够自动生成小朋友算术练习题目的程序。设计一个程岁亏则空尘序,要求能够随机生成每个题目,并具有以下基本功能:

 * (1)要求题目以单项选择题及填空题两种形式出现。

 * (2)完成一题后自动判断答案是否正确,分别d出不同的提示,并自动更新成下一题。

 * (3)单击“退出”时,汇总本次计算结果,共完成几题,正确率,并给出相应评乎棚语。确定后正式退出。

 */

public class publicDemo2 {

public static void main(String[] args) {

Random random = new Random()

StringBuilder stringBuilder = new StringBuilder()

String[] symbol = {"+","-","*","/"}

String question_tip = "选择题(除法保留小数点后两位):"

stringBuilder.append(question_tip).append("\n")

System.out.println(question_tip)

int a

int b

String f

String result_str

for (int i=1i<11i++){

a = random.nextInt(50)

b = random.nextInt(50)

f = symbol[random.nextInt(3)]

String question = i+"、"+a+f+b+"="

System.out.println(question)

stringBuilder.append(question)

Scanner sc = new Scanner(System.in)

String sc_str = sc.next()

if("/".equals(f)){

double scan_value = Double.parseDouble(sc_str)

double result_value = a / b

BigDecimal bigDecimal1 = new BigDecimal(scan_value)

scan_value = bigDecimal1.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue()

BigDecimal bigDecimal2 = new BigDecimal(result_value)

result_value = bigDecimal2.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue()

result_str = (scan_value==result_value)?"回答正确!":"回答错误!"

stringBuilder.append(scan_value).append("(答案:"+result_value+")"+result_str).append("\n")

}else{

int scan_value = Integer.parseInt(sc_str)

int result_value

if("+".equals(f)){

result_value = a + b

}else if("-".equals(f)){

result_value = a - b

}else if("*".equals(f)){

result_value = a * b

}else{

break

}

result_str = (scan_value==result_value)?"回答正确!":"回答错误!"

stringBuilder.append(scan_value).append("(答案:"+result_value+")"+result_str).append("\n")

}

}

System.out.println(stringBuilder.toString())

}

}


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/yw/12440329.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-25
下一篇 2023-05-25

发表评论

登录后才能评论

评论列表(0条)

保存