第二次作业

- 题目:
请编写一个能自动生成小学四则运算题目的 “软件”。
让程序能接受用户输入答案,并判定对错。 
最后给出总共 对/错 的数量。

- 需求分析

编写一个可以输入数据并对其进行计算的程序

判断用户的答案是否正确

给出做对/错提的数量


- 设计

1.用Java语言来编写

2.从键盘输入数据及运算符,分别编写对应每个运算符的子函数,判断对错

3.输出运算结果


- 代码实现

import java.text.DecimalFormat;
import java.util.Scanner;

public class yhgc1 {
public static void main(String[] args) {
String condition = "";
yhgc1 zuoye = new yhgc1();
do {
Scanner scanner = new Scanner(System.in);
try {
System.out.print("第一个数:");
double x = scanner.nextDouble();
System.out.print("运算符:");
String s = scanner.next();
System.out.print("第二个数:");
double y = scanner.nextDouble();

char z = s.charAt(0);
zuoye.jisuan(x, y, z);
} catch (Exception e) {
System.out.println("请输入正确的数据!");
}
System.out.print("是否与您计算的值相同?");
condition = scanner.next();
} while ("continue".equals(condition));
}

public static void jisuan(double x, double y, Character z) {
DecimalFormat r = new DecimalFormat();
r.applyPattern("#0.00");
if (z.equals('+')) {

System.out.println( "结果为:" + r.format((x + y)));
} else if (z.equals('-')) {
System.out.println("结果为:" + r.format((x - y)));
} else if (z.equals('*')) {
System.out.println("结果为:" + r.format((x * y)));
} else if (z.equals('/')) {
if (y == 0) {
System.out.println("被除数不能为0");
} else {
System.out.println("结果为:" + r.format((x / y)));
}
} else {
System.out.println("无法识别改运算符");
}
}
}


- 测试

 


- 事后分析和总结

分析题目能有一些谱,能够想到怎么应该怎么做但是着手写程序时不知道怎么弄,编程能力很差。

 

posted on 2015-04-12 20:57  张义平  阅读(106)  评论(0编辑  收藏  举报

导航