码迷,mamicode.com
首页 > 其他好文 > 详细

第四次作业

时间:2015-10-24 20:26:22      阅读:187      评论:0      收藏:0      [点我收藏+]

标签:

package CSniu;

import java.awt.HeadlessException;
import java.awt.Label;
import java.util.Random;
import java.util.Scanner;

public class Copy
/*
* 复制的源代码,所以直接把类名叫Copy了。 修改较大,将一些常用的封装成了方法。
* 至于增量是用的李思豪的代码,因为李思豪的代码早先已经实现了几个要求。 感觉比较省事些。
* 老师要求的那个让用户自己选择随机数生成的范围,我们并没有做,原因是我们用的命令行界面,选来选去已经很繁琐了,所以我们决定并没有写。希望老师理解。
*/
{
public int answer = 0;
private static int mistake = 0;
private static int correct = 0;
private static int select;
private int num;
private Random random;
private int tf;

public static void main(String[] args)
{
// 调用计算器方法。
Copy copy = new Copy();
copy.calculator();
}

private int inPut()
{
/*
* 创建input方法,用来接收并检测用户输入的数据
*
*/
while (true)
{

try
{
Scanner scanner = new Scanner(System.in);
answer = scanner.nextInt();
break;
}
catch (Exception e)
{
System.out.println("請輸入一個整數。");
}
}
return answer;
}

public void calculator()
{
Copy copy = new Copy();
Scanner scanner = new Scanner(System.in);
random = new Random();
System.out.print("请输入你想出的题目数量:");
num = scanner.nextInt();
System.out.println("请输入你想要的运算符.0 加,1 减,2 乘,3 除.4,随机 ");
select = scanner.nextInt();
System.out.println("是否混合运算?1,是。2,否。");
tf = scanner.nextInt();
System.out.println("请输入你规定的时间,以分钟为单位。");
long longTime = System.currentTimeMillis() + scanner.nextInt() * 10000;
new Thread()
{
/*
* 启用一个子线程,用来实现倒计时效果,不过我们两个并没有找到在煮程序已经运行完的时候把子线程关闭的方法,这里有一个BUG。
*
*/
public void run()
{
while (true)
{
long l = System.currentTimeMillis();
if (l == longTime)
{
System.out.println("时间到!!");
copy.score();
copy.exit(0);
}
else
{
continue;
}
}
}
}.start();
for (int i = 0; i < num; i++)
// 用for循环来设置出题数量,既用户输入的num值。
{
// 调用出题方法。
if (tf == 2)
{
//用来判断是否选择了混合运算。
Switch(select);
}
else
{
aSMD();
}

}
score();
}

public void score()
{
/*
* 统计得分方法,用来判断用户输入的结果是否正确 以及是否有没做的。
*/
System.out.println("你一共作对" + correct + "道。");
System.out.println("你一共作错" + mistake + "道。");
if ((num - (mistake + correct)) != 0)
{
System.out.println("你一共作对了" + correct + "道,错了" + mistake + "道,没做" + (num - (mistake + correct)) + "道。");
}
else if (mistake == 0 && correct != 0)
{
System.out.print("你太聪明了!!^.^");
}
else if (correct > mistake)
{
System.out.print("再接再厉!^.^");
}
else if (correct > mistake)
{
System.out.print("再接再厉!^.^");
}
else if (mistake > correct)
{
System.out.print("不要灰心,再来一次吧。");
}
else if (mistake == 0 && correct == 0)
{
System.out.print("同学,你没做题哦!");
}
System.exit(0);

}

public void Switch(int select)
{
int x;
int y;
int[] a=new int[10] ;// 定义a用来存储x的值,用作下一次比较。如果相等,则重新随机。保证了已经出现的题目不会再重新出现。
int[] b =new int [10];
Label1: while (true)
{

x = random.nextInt(11);
y = random.nextInt(11);
for (int i = 0; i < a.length; i++)
{
if (x == a[i] & y == b[i])
{
continue;
}
else
{
a[i] = x;
b[i] = y;
break Label1;
}
}
}
if (select == 4)
{
/*
* 这里设置if语句,用来判断用户是否选择了随机模式。
*
*/
select = random.nextInt(4);
}
switch (select)
// 根据用户选择的运算符号进行出题。
{
case 0:
System.out.println("请输入 " + x + "+" + y + " 的正确答案:");
answer = inPut();
if (answer != (x + y))
// 用来判断用户输入的答案是否与正确答案相符合,是,正确+1,否,错误+1.
{
mistake++;
}
else
{
correct++;
}
break;
case 1:
if (x < y)
{
System.out.print("请输入 " + y + "-" + x + " 的正确答案:");
answer = inPut();
if (answer != (y - x))
{
mistake++;
}
else
{
correct++;
}
break;
}
System.out.println("请输入 " + x + "-" + y + " 的正确答案:");
answer = inPut();
if (answer != (x - y))
{
mistake++;
}
else
{
correct++;
}
break;
case 2:
System.out.println("请输入 " + x + "*" + y + " 的正确答案:");
answer = inPut();
if (answer != (x * y))
{
mistake++;
}
else
{
correct++;
}
break;
case 3:
if (x == 0)
// 因为被除数不能为0,所以当被除数为0时,在这里进行掉转,让0为除数。
{
System.out.println("请输入 " + y + "/" + x + " 的正确答案:");
answer = inPut();
}
System.out.println("请输入 " + x + "/" + y + " 的正确答案:");
answer = inPut();
if (answer != (x / y))
{
mistake++;
}
else if (answer != 0)
{
mistake++;
}
else
{
correct++;
}
break;
}
}

public void exit(int x)
{
/*
* 创建退出方法,被子线程调用。
*
*/
System.exit(x);
}

public void aSMD()
{
Scanner scanner = new Scanner(System.in);
int x;
int y;
int z;
int a[] = new int[10];
int b[] = new int[10];
int c[] = new int[10];
Label1: while (true)
{
//设置循环,用来判断是否和上一次重复,如果重复,则重新随机。
x = random.nextInt(11);
y = random.nextInt(11);
z = random.nextInt(11);
for (int i = 0; i < a.length; i++)
{
if (x == a[i] & y == b[i] & z == c[i])
{
continue;
}
else
{
a[i] = x;
b[i] = y;
c[i] = z;
break Label1;
}
}
}
int s = random.nextInt(4);
for (int i = 0; i < 4; i++)
{
switch (s)
{
case 0:

System.out.println("请输入" + x + "+" + "(" + y + "*" + z + ")" + "的结果;");
answer = scanner.nextInt();
if (answer != x + (y * z))
{
mistake++;
}
else
{
correct++;
}
break;

case 1:
if (y == 0)
{
System.out.println("请输入" + x + "+" + "(" + z + "/" + y + ")" + "的结果;");
answer = scanner.nextInt();
if (answer != x + (z / y))
{
mistake++;
}
else
{
correct++;
}
break;
}
System.out.println("请输入" + x + "+" + "(" + y + "/" + z + ")" + "的结果;");
answer = scanner.nextInt();
if (answer != x + (y / z))
{
mistake++;
}
else
{
correct++;
}
break;
case 2:
if (x < (y * z))
{
System.out.println("请输入" + "(" + y + "*" + z + ")" + "-" + x + "的结果;");
answer = scanner.nextInt();
if (answer != (y * z) - x)
{
mistake++;
}
else
{
correct++;
}
break;
}
System.out.println("请输入" + x + "-" + "(" + y + "*" + z + ")" + "的结果;");
answer = scanner.nextInt();
if (answer != x - (y * z))
{
mistake++;
}
else
{
correct++;
}
break;
case 3:
if (y == 0)
{
System.out.println("请输入" + x + "-" + "(" + z + "/" + y + ")" + "的结果;");
answer = scanner.nextInt();
if (answer != x + (z / y))
{
mistake++;
}
else
{
correct++;
}
break;
}
System.out.println("请输入" + x + "-" + "(" + y + "/" + z + ")" + "的结果;");
answer = scanner.nextInt();
if (answer != x - (y / z))
{
mistake++;
}
else
{
correct++;
}
}
}
}
}

技术分享

PSP耗时分析:
需求分析:0h
设计思路:0.2h
代码实现:0.2h
测试:0.02h

和李思豪同学一块做的。

第四次作业

标签:

原文地址:http://www.cnblogs.com/66668888xin/p/4907432.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!