Java Web ClassProgram_2

问题描述

1、生成简单的四则运算题目

2、要求问题不能重复,除法运算生成假分数

代码实现

Data.class

import java.util.Random;


public class Data {
int x;
int y;
char a;
String result;
public boolean equals(Data d){
if(d.x==this.x&&d.y==this.y&&this.a==d.a){
return true;
}
else{
return false;
}
}
/**
*
* @param border 两个数字的范围
* @param grade 计算符的等级难度
*/
public void getData(int border,int grade){
Random ran=new Random();
x=ran.nextInt(border);
y=ran.nextInt(border);
this.setChar(ran.nextInt(grade));
}

private void setChar(int grade){
if(grade==0){
a='+';
result=(x+y)+"";
}
else if(grade==1){
a='-';
result=(x-y)+"";
}
else if(grade==2){
a='*';
result=(x*y)+"";
}
else if(grade==3){
a='/';
int left=x/y;
int up=x%y;
int down=y;
int k=1;
for(int i=1;i<=up;i++){
if(up%i==0&&down%i==0){
k=i;
}
}
up=up/k;
down=down/k;
if(up!=0&&left!=0){
result=left+" "+up+"/"+down;
}
else if(left==0){
result=up+"/"+down;
}
else if(up==0){
result=left+"";
}
else{
result=0+"";
}
}
else if(grade==4){
a='%';
result=x*y+"";
}
}
public String toString(){

return x+""+a+y+"="+result;

}
}

ClassTest.class


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

public class ClassTest {
LinkedList<Data> list=new LinkedList<Data>();
int number;
int border;
int grade;
public ClassTest(int number,int border,int grade){
this.number=number;
this.border=border;
this.grade=grade;
}
public void getData(){
for(int i=0;i<number;i++){
Data data=new Data();
data.getData(border, grade);
int flag=0;
for(int k=0;k<list.size();k++){
if(data.equals(list.get(k))){
flag=1;
i--;
break;
}
}
if(flag==0){
list.add(data);
}
}
}
public void show(){
for(int i=0;i<list.size();i++){
System.out.println(i+"道题目");
System.out.println(list.get(i));
}
}
public static void main(String[] args) {
Scanner scan=new Scanner(System.in);
System.out.println("输入一共多少道题目");
int number=scan.nextInt();
ClassTest test=new ClassTest(number,100,4);
test.getData();
test.show();
}
}

代码存在问题:

  1、代码复用性存在问题,整体结构为之后功能扩充不利

  2、对于项目的完善需要相当多的代码支持,需要一定的工作量解决

posted @ 2017-03-06 21:30  runningboys  阅读(123)  评论(0编辑  收藏  举报