首 页 行业资讯 新车 试驾评测 养车用车 车型库

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

发布网友 发布时间:2022-04-21 22:00

我来回答

3个回答

热心网友 时间:2023-08-08 04:52

花了好几个小时,测试通过,希望楼主多给点分
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]!=null&&i<questionNumber;i++){
if((question.getFirstParam()==questions[i].getFirstParam())&&(question.getSecondParam()==questions[i].getSecondParam())){
continue mywhile;
}
}
for(int i=0;i<questionNumber;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;i<number;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()+"分");
}
}

热心网友 时间:2023-08-08 04:52

有做过 html+JS的。

热心网友 时间:2023-08-08 04:53

还要出题啊,又要动脑筋 啊。。。。

声明声明:本网页内容为用户发布,旨在传播知识,不代表本网认同其观点,若有侵权等问题请及时与本网联系,我们将在第一时间删除处理。E-MAIL:11247931@qq.com