点滴成就 学习时间 新编写代码行数 博客量 学到知识点
第一周 11 0 0 了解软件工程
第二周 11 0 1 了解软件工程
第三周 11 0 1 用例图
第四周 12 25 0 结对编程
第五周 12 30 1 类图
第六周 12 60 0 软件工程
第七周 13 40 1 测试

package com.mindu.FourArithmeticOperations;

import java.util.Random;
import java.util.Scanner;

public class test {

static int standardAnswer;

public static void add(int addend1,int addend2){//加法
System.out.print(addend1 + "+" + addend2 + "=");
standardAnswer = addend1 + addend2;
}
public static void subtract(int minuend,int subtractor){//减法
if(minuend<subtractor){//减数大于被减数时交换
System.out.print(subtractor + "-" + minuend + "=");
standardAnswer = subtractor - minuend;
}
else{
System.out.print(minuend + "-" + subtractor + "=");
standardAnswer = minuend - subtractor;
}
}
public static void multiply(int multiplicator1,int multiplicator2){//乘法
System.out.print(multiplicator1 + "×" + multiplicator2 + "=");
standardAnswer = multiplicator1 * multiplicator2;
}
public static void divide(int dividend,int divisor){//除法
if(dividend<divisor||divisor == 0){//被除数小于除数或除数为0时两个交换
while(divisor%dividend>0){//有余数时进入循环
dividend--;
}
System.out.print(divisor + "÷" + dividend + "=");
standardAnswer = divisor / dividend;
}
else{
while(divisor%dividend>0){//有余数时进入循环
dividend--;
}
System.out.print(dividend + "÷" + divisor + "=");
standardAnswer = dividend / divisor;
}
}

public static void compared(int myAnswer,int standardAnswer){//判断是否为正确答案
if(myAnswer == standardAnswer){
System.out.println("做对啦!");
}
else{
System.out.println("做错啦!再接再厉!");
}
}

public static void main(String[] args) {
// TODO Auto-generated method stub
Random random = new Random();
Scanner input=new Scanner(System.in);
int myAnswer;

System.out.print("请输入做题数量:");
int questionCount = input.nextInt();
System.out.println();

for(int i = 0;i<questionCount;i++){
int num1 = random.nextInt(101);//随机生成整数
int num2 = random.nextInt(101);
int index = random.nextInt(4);

System.out.print((i+1)+".");//题号

switch(index){
case 0://+
add(num1,num2);
myAnswer = input.nextInt();
compared(myAnswer,standardAnswer);
break;
case 1://-
subtract(num1,num2);
myAnswer = input.nextInt();
compared(myAnswer,standardAnswer);
break;
case 2://*
multiply(num1,num2);
myAnswer = input.nextInt();
compared(myAnswer,standardAnswer);
break;
case 3:// /
divide(num1,num2);
myAnswer = input.nextInt();
compared(myAnswer,standardAnswer);
break;
default:
break;
}
}
System.out.print("已经做完"+questionCount+"题咯!");
input.close();
}

}

运行截图:

posted on 2016-04-08 15:44  MinDuu  阅读(112)  评论(0编辑  收藏  举报