完成一个计算器
阶段1:1、定义主界面菜单,
1、参数设置
2、小学二年级口算题
3、小学三年级口算题
4、小学四年级口算题
5、错题集其中参数设置包括出题个数(缺省值为30)、操作数数值范围。
要求:小学二年级口算题操作数为两个、可进行加减乘除运算(除法必须可以整除),操作数范围不超过100.小学三年级口算题操作数不超过4个,可以进行加减乘除,操作数范围不超过1000.(要求采用继承小学二年级出题类的方式,实现小学三年级的出题类)小学四年级口算题操作数不超过5个,可以进行加减乘除,还可以加入括号运算。 (要求采用继承小学三年级出题类的方式,实现小学四年级的出题类)
创建一个text类:写入主函数
package jyk.ui;
import java.awt.*;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Random;
import java.util.Scanner;
public class test {
static ArrayList<formula> list = new ArrayList<>();
static ArrayList<str> listSrt = new ArrayList<>();
//主函数
public static void main(String args[]) {
Menu menu = new Menu();
Scanner sc = new Scanner(System.in);
while (true) {
menu.putMenu();
Grade2 g2 = new Grade2();
Grade3 g3 = new Grade3();
Grade4 g4 = new Grade4();
int choose = sc.nextInt();
switch (choose) {
case 1:
System.out.println("请输入出题个数");
int i = sc.nextInt();
Grade2.setQuantity(i);
Grade3.setQuantity(i);
Grade4.setQuantity(i);
System.out.println("请输入取值范围");
int j = sc.nextInt();
Grade2.setBound(j);
Grade3.setBound(j);
Grade4.setBound(j);
break;
case 2:
g2.putIt();
break;
case 3:
g3.putIt();
break;
case 4:
g4.putIt();
break;
case 5:
Grade2 g = new Grade2();//错题
}
}
}
// System.out.println("小数精确计算:");
// System.out.println("请输入两个小数");
// Scanner sc = new Scanner(System.in);
// double d1 = sc.nextDouble();
// BigDecimal b1= new BigDecimal(String.valueOf(d1));
// double d2 = sc.nextDouble();
// BigDecimal b2= new BigDecimal(String.valueOf(d2));
// System.out.println("减法非精确计算结果为:" + (d1 - d2));
// System.out.println("精确计算结果为:" + b1.subtract(b2));
//
// System.out.println("大数精确计算:");
// System.out.println("请输入两个大数");
// String d11 = sc.next();
// BigDecimal b11= new BigDecimal(d11);
// String d22 = sc.next();
// BigDecimal b22= new BigDecimal(d22);
// System.out.println("加法精确计算结果为:" + b11.add(b22));
// System.out.println("减法精确计算结果为:" + b11.subtract(b22));
// System.out.println("乘法精确计算结果为:" + b11.multiply(b22));
// System.out.println("除法精确计算结果为:" + b11.divide(b22));
public static void wrongBook() {
for (int i = 0; i < listSrt.size(); i++) {
if(listSrt.get(i).wrongTimes>0){
System.out.println(listSrt.get(i).st+"=");
Scanner sc = new Scanner(System.in);
double d = sc.nextDouble();
if(d==listSrt.get(i).result) {
System.out.println("正确");
listSrt.get(i).wrongTimes=0;
}
else {
System.out.println("错误");
listSrt.get(i).wrongTimes++;
System.out.println("该题错误" + listSrt.get(i).wrongTimes + "次");
System.out.println("正确答案为:"+listSrt.get(i).result);
}
}
}
}
public static void setRandom(Grade2 data){
Random random = new Random(data.getQuantity() + data.getElement()%(data.getBound()));
for (int i = 0; i < data.getQuantity(); i++) {
int j=random.nextInt();
if(!judgment(list,j))
{
formula f= new formula(j);
list.add(f);
}else
i--;
}
}
public static boolean judgment (ArrayList<formula> list,int j){
for (int i = 0; i < list.size(); i++) {
formula fo = list.get(i);
if(fo.random == j){
return true;
}
}
return false;
}
public static boolean isNumber(String str) {
for (int i = 0; i < str.length(); i++) {
if (!Character.isDigit(str.charAt(i)) && str.charAt(i) != '.' && str.charAt(i) != ' ') {
return false;
}
}
return true;
}
public static Double getResult(String str) {
if (str.isEmpty() || isNumber(str)) {
return str.isEmpty() ? 0 : Double.parseDouble(str);
}
if (str.contains(")")) {
int lIndex = str.lastIndexOf("(");
int rIndex = str.indexOf(")", lIndex);
return getResult(str.substring(0, lIndex) + getResult(str.substring(lIndex + 1, rIndex)) + str.substring(rIndex + 1));
}
if (str.contains("+")) {
int index = str.lastIndexOf("+");
return getResult(str.substring(0, index)) + getResult(str.substring(index + 1));
}
if (str.contains("-")) {
int index = str.lastIndexOf("-");
return getResult(str.substring(0, index)) - getResult(str.substring(index + 1));
}
if (str.contains("*")) {
int index = str.lastIndexOf("*");
return getResult(str.substring(0, index)) * getResult(str.substring(index + 1));
}
if (str.contains("/")) {
int index = str.lastIndexOf("/");
return getResult(str.substring(0, index)) / getResult(str.substring(index + 1));
}
return null;
}
public static int getFactor(int d){
Random r = new Random();
int i = r.nextInt(100);
int []a = new int[100];
int n = 0;
for(int j = 1;j<=d;j++){
if(d%j == 0) {
a[n] = j;
n++;
}
}
int result = a[i%n];
return result;
}
}起到主要计算作用;
#写Menu类//主菜单:
package jyk.ui;
public class Menu {
public void putMenu(){
System.out.println("1、参数设置");
System.out.println("2、小学二年级口算题");
System.out.println("3、小学三年级口算题");
System.out.println("4、小学四年级口算题");
System.out.println("5、错题集");
}
public Menu(){
}
}写Data类,完成设置要求:
package jyk.ui;
import java.util.Scanner;
public class Data {
int quantity;
int mode;
int element;
int MulAndDiv;
int bracket;
int bound;
public Data() {
Scanner sc = new Scanner(System.in);
System.out.println("请输入打印数量:");
this.setQuantity(sc.nextInt());
System.out.println("请选择打印方式:");
this.setMode(sc.nextInt());
System.out.println("请输入操作数的个数");
this.setElement(sc.nextInt());
System.out.println("是否有乘除法(0/1)");
this.setMulAndDiv(sc.nextInt());
System.out.println("是否有括号(0/1)");
this.setBracket(sc.nextInt());
System.out.println("请输入取值范围");
this.setBound(sc.nextInt());
}
public Data(int quantity, int mode, int element, int mulAndDiv, int bracket, int bound) {
this.quantity = quantity;
this.mode = mode;
this.element = element;
this.MulAndDiv = mulAndDiv;
this.bracket = bracket;
this.bound = bound;
}
public int getQuantity() {
return quantity;
}
public void setQuantity(int quantity) {
this.quantity = quantity;
}
public int getMode() {
return mode;
}
public void setMode(int mode) {
this.mode = mode;
}
public int getElement() {
return element;
}
public void setElement(int element) {
this.element = element;
}
public int isMulAndDiv() {
return MulAndDiv;
}
public void setMulAndDiv(int mulAndDiv) {
MulAndDiv = mulAndDiv;
}
public int isBracket() {
return bracket;
}
public void setBracket(int bracket) {
this.bracket = bracket;
}
public int getBound() {
return bound;
}
public void setBound(int bound) {
this.bound = bound;
}
}
# 建
Attribute类完成设置的返回:
package jyk.ui;
public class Attribute {
static int quantity = 30;
static int mode = 3;
static int element;
static int MulAndDiv;
static int bracket = 1;
static int bound;
public static int getQuantity() {
return quantity;
}
public static void setQuantity(int quantity) {
Attribute.quantity = quantity;
}
public static int getMode() {
return mode;
}
public static void setMode(int mode) {
Attribute.mode = mode;
}
public static int getElement() {
return element;
}
public static void setElement(int element) {
Attribute.element = element;
}
public static int getMulAndDiv() {
return MulAndDiv;
}
public static void setMulAndDiv(int mulAndDiv) {
MulAndDiv = mulAndDiv;
}
public static int getBracket() {
return bracket;
}
public static void setBracket(int bracket) {
Attribute.bracket = bracket;
}
public static int getBound() {
return bound;
}
public static void setBound(int bound) {
Attribute.bound = bound;
}
public Attribute() {
}
}
#建
formula类,完成随机数:
package jyk.ui;
public class formula {
int random;
public formula() {
}
public formula(int random) {
this.random = random;
}
public int getRandom() {
return random;
}
public void setRandom(int random) {
this.random = random;
}
}
#建
Grade2类,实现二年级:
package jyk.ui;
import java.util.Random;
import java.util.Scanner;
import static jyk.ui.test.*;
public class Grade2 {
static int quantity = 30;
int mode = 3;
int element;
int MulAndDiv = 1;
int bracket ;
static int bound = 100;
public int getQuantity() {
return quantity;
}
public static void setQuantity(int q) {
quantity = q;
}
public int getMode() {
return mode;
}
public void setMode(int mode) {
this.mode = mode;
}
public int getElement() {
return element;
}
public void setElement(int element) {
this.element = element;
}
public int getMulAndDiv() {
return MulAndDiv;
}
public void setMulAndDiv(int mulAndDiv) {
MulAndDiv = mulAndDiv;
}
public int getBracket() {
return bracket;
}
public void setBracket(int bracket) {
this.bracket = bracket;
}
public int getBound() {
return bound;
}
public static void setBound(int b) {
bound = b;
}
{
element = 2;
bracket = 0;
}
public void putIt(){
setRandom(this);
int m,n,wrongTimes = 0;
int p=0,h=0;
char[]a = {'+', '-', '*', '/'};
char[]b = {'+','-'};
int []number=new int[100];
if(this.MulAndDiv == 1) {//有乘除
for (int i = 0; i < this.quantity; i++) {
Random random=new Random();//list.get(i).getRandom()
str s = new str();
for (int j = 0; j < this.element; j++) {
number[j] = random.nextInt(this.bound);
}
System.out.printf("%d、", i+1);//序号
System.out.printf("%d",number[0]);
s.st = s.st + number[0];
for (int k = 1; k < this.element; k++) {//打印
n = random.nextInt(100);//随机括号
m = random.nextInt(4);//随机符号
System.out.printf("%c",a[m]);
s.st = s.st + a[m];
if(h==1)
h++;
if(n%2==0&&k< this.element-1&&p==0) {
System.out.printf("(");
s.st = s.st + "(";
p=1;//左括号已存在
h++;
}
if(h==2) {
if(m == 3) {
int l = getFactor(number[k-1]);
System.out.printf("%d)", l );
s.st = s.st + l + ")";
p=0;
h=0;
}
else {
System.out.printf("%d)", number[k]);
s.st = s.st + number[k] + ")";
p = 0;
h = 0;
}
}
else {
if(m == 3) {
int l = getFactor(number[k-1]);
System.out.printf("%d",l );
s.st = s.st + l;
}
else {
System.out.printf("%d", number[k]);
s.st = s.st + number[k];
}
}
}
s.result = getResult(s.st);
listSrt.add(s);
System.out.printf("=");
Scanner sc = new Scanner(System.in);
double d = sc.nextDouble();
if(d==listSrt.get(i).result)
System.out.println("正确");
else {
System.out.println("错误");
wrongTimes++;
s.wrongTimes++;
System.out.println("该题错误" + s.wrongTimes + "次");
System.out.println("正确答案为:"+s.result);
}
// if((i+1) % data.mode==0)//打印格式
// System.out.printf("\n");
}
}
System.out.println("正确率为" + (this.quantity-wrongTimes) * 100 / this.quantity + "%");
while (true){
System.out.println("是否继续练习错题(0/1):");
Scanner sc = new Scanner(System.in);
if(sc.nextInt() == 1){
wrongBook();
}
else break;
}
}
public Grade2() {
}
}
#建
Grade3类,三年级继承二年级:
package jyk.ui;
public class Grade3 extends Grade2 {
{
element = 4;
bracket = 0;
}
public Grade3(){
}
}
#建
Grade4类,四年级继承三年级:
package jyk.ui;
public class Grade4 extends Grade3{
{
element = 5;
bracket = 1;
}
public Grade4(){
}
}
#建
str类,错题:
package jyk.ui;
public class str {
String st;
double result;
int wrongTimes;
public String getSt() {
return st;
}
public void setSt(String st) {
this.st = st;
}
public int getWrongTimes() {
return wrongTimes;
}
public void setWrongTimes(int wrongTimes) {
this.wrongTimes = wrongTimes;
}
public str(String st, double result, int wrongTimes) {
this.st = st;
this.result = result;
this.wrongTimes = wrongTimes;
}
public double getResult() {
return result;
}
public void setResult(double result) {
this.result = result;
}
public str(String str, double result) {
this.st = str;
this.result = result;
}
public str(String str) {
this.st = str;
}
public String getStr() {
return st;
}
public void setStr(String str) {
this.st = str;
}
public str() {
st = "";
result = 0;
wrongTimes = 0;
}
}基本实现要求:
阶段2:1、学生实时答题结束后,可以选择是否进行下一套题目答题,如果选择是,则抽取下一套题进行答题,答题结束可以通过查看错题本,查询今日做题正确率。2、也可以针对错题进行二次答题。
#建
str类,错题:
package jyk.ui;
public class str {
String st;
double result;
int wrongTimes;
public String getSt() {
return st;
}
public void setSt(String st) {
this.st = st;
}
public int getWrongTimes() {
return wrongTimes;
}
public void setWrongTimes(int wrongTimes) {
this.wrongTimes = wrongTimes;
}
public str(String st, double result, int wrongTimes) {
this.st = st;
this.result = result;
this.wrongTimes = wrongTimes;
}
public double getResult() {
return result;
}
public void setResult(double result) {
this.result = result;
}
public str(String str, double result) {
this.st = str;
this.result = result;
}
public str(String str) {
this.st = str;
}
public String getStr() {
return st;
}
public void setStr(String str) {
this.st = str;
}
public str() {
st = "";
result = 0;
wrongTimes = 0;
}
标签:01,int,四则运算,System,st,str,动脑,public,out From: https://www.cnblogs.com/jingyk/p/16770027.html