结对编程——四则运算

题目:某公司程序员二柱的小孩上了小学二年级,老师让家长每天出30道(100以内)四则运算题目给小学生做。

采用语言:Java

结对对象:   张莲     博客地址:http://home.cnblogs.com/u/linuoxin/

双方贡献比例:1:1

编程照片:

 

源代码:

package jiedui;

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

public class Jiedui {

public static void main(String[] args) {
// TODO Auto-generated method stub

double result1 = -1;// 正确的运算结果
double result2 = -1;// 用户输入的运算结果

// 循环30次,做30次运算
for (int i = 1; i <= 30; i++) {
System.out.println("第"+i+"题");
result1 = rightResult();
result2 = inputResult();
// 判断用户输入的答案是否正确
if (result1 == result2) {
System.out.println("恭喜你!答对了。");
} else {
System.out.println("噢~,答错了。");
System.out.println("正确答案为:" + result1);
}
}
}

public static double rightResult() {
int operand1 = -1;// 运算数1
int operand2 = -1;// 运算数2
int operation = -1;// 用于标记运算符:0为+,1为-,2为*,3为/
double result = -1;// 正确的运算结果

Random random = new Random();

operand1 = random.nextInt(100);// 随机生成在[0,100]范围内的运算数1
operand2 = random.nextInt(100);// 随机生成在[0,100]范围内的运算数2
operation = random.nextInt(3);// 随机生成运算符对应的序号:0为+,1为-,2为*,3为/
switch (operation) {
case 0: {
result = operand1 + operand2;
System.out.print(operand1 + "+" + operand2 + "=");// 输出题目
break;
}
case 1: {
result = operand1 - operand2;
System.out.print(operand1 + "-" + operand2 + "=");
break;
}
case 2: {
result = operand1 * operand2;
System.out.print(operand1 + "*" + operand2 + "=");
break;
}
case 3: {
if (operand2 == 0) {
operand2 = random.nextInt(99)+1;//运算数2重新生成1~100的数
}
result = operand1 / operand2;
System.out.print(operand1 + "/" + operand2 + "=");
break;
}
}

return result;
}

public static double inputResult() {
double result = -1;
Scanner input = new Scanner(System.in);
result = input.nextDouble();// 用户输入答案
return result;
}

}

运行结果:

 

总结:

       结对编程让做题效率明显有效的提高,出错率降低,效果很好

学习进度条:

点滴成就

学习时间

新编写代码行数

博客量(篇)

学到的知识点

第一周

2h

0

0

了解认识软件工程

第二周

2h

0

1

了解软件工程思想

第三周

2h

0

1

制作并进行问卷调查

第四周

4h

106

1

需求分析与结对编程

第五周

4h

239

0

Java的学习

第六周

3h

0

0

初步学习“软件测试”

第七周

4h

106

1

软件系统设计、结对编程

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
原文地址:https://www.cnblogs.com/linuoxin/p/5373863.html