软件工程个人作业03
软件加强版
代码:
//随机运算
//HanZhao 2017.3.4
import java.util.*;
public class test {
public static void main(String[] args) {
// TODO 自动生成的方法存根
char ch='y';
while(ch=='y'){
System.out.println("1、是否有乘除法");
System.out.println("2、是否有括号");
System.out.println("3、数值范围");
System.out.println("4、加减有无负数");
System.out.println("5、除法有无余数");
Scanner scan=new Scanner(System.in);
int choose=scan.nextInt();
switch(choose)
{
case 1:{
chengchufa();
break;
}
case 2:{
kuohao();
break;
}
case 3:{
fanwei();
break;
}
case 4:{
fushu();
break;
}
case 5:{
yushu();
break;
}
default:
System.out.println("输入错误");
break;
}
}
}
public static void dingzhi(){
System.out.println("请输入定制的题数:");//定制练习量
Scanner scan=new Scanner(System.in);
int n=scan.nextInt();
System.out.println("1、整数练习");//选择题型
System.out.println("2、分数练习");
System.out.println("请选择:");
Scanner scan1=new Scanner(System.in);
int s=scan1.nextInt();
String [] x=new String[n];
String [] y=new String[n];
switch(s){
case 1:{
int i=0;
boolean flag;
while(i<n)
{
int a=(int)(Math.random()*100);
int b=(int)(Math.random()*100);
int c=(int)(Math.random()*4);
x[i]=new String();
if(c==0)
x[i]=a+"+"+b+"=";
if(c==1)
x[i]=a+"-"+b+"=";
if(c==2)
x[i]=a+"*"+b+"=";
if(c==3)
x[i]=a+"/"+b+"=";
flag=true;
for(int k=0;k<i;k++)
{
if(x[k].equals(x[i]))
{
flag=false;
break;
}
}
if(flag)
{
System.out.println(x[i]);
i++;
}
}
break;
}
case 2:{
int j=0;
boolean flag;
while(j<n)
{
int x1=(int) (Math.random()*100);
int y1=(int) (Math.random()*100);
int x2=(int) (Math.random()*100);
int y2=(int) (Math.random()*100);
int z=(int) (Math.random()*4);
y[j]=new String();
if(x1<y1||x2<y2)
{
if(z==0)
y[j]="("+x1+"/"+y1+")+"+"("+x2+"/"+y2+")=";
if(z==1)
y[j]="("+x1+"/"+y1+")-"+"("+x2+"/"+y2+")=";
if(z==2)
y[j]="("+x1+"/"+y1+")*"+"("+x2+"/"+y2+")=";
if(z==3)
y[j]="("+x1+"/"+y1+")/"+"("+x2+"/"+y2+")=";
flag=true;
for(int l=0;l<j;l++)
{
if(y[l].equals(y[j]))
{
flag=false;
break;
}
}
if(flag)
{
System.out.println(y[j]);
j++;
}
}
break;
}
}
}
}
public static void chengchufa(){
System.out.println("1、有乘除法");
System.out.println("2、无乘除法");
Scanner choice=new Scanner(System.in);
int xuanze=choice.nextInt();
if(xuanze==1)
{
dingzhi();
}
else
{
System.out.println("请输入定制的题数:");//定制练习量
Scanner scan=new Scanner(System.in);
int n=scan.nextInt();
System.out.println("1、整数练习");//选择题型
System.out.println("2、分数练习");
System.out.println("请选择:");
Scanner scan1=new Scanner(System.in);
int s=scan1.nextInt();
String [] x=new String[n];
String [] y=new String[n];
switch(s){
case 1:{
int i=0;
boolean flag;
while(i<n)
{
int a=(int)(Math.random()*100);
int b=(int)(Math.random()*100);
int c=(int)(Math.random()*2);
x[i]=new String();
if(c==0)
x[i]=a+"+"+b+"=";
if(c==1)
x[i]=a+"-"+b+"=";
flag=true;
for(int k=0;k<i;k++)
{
if(x[k].equals(x[i]))
{
flag=false;
break;
}
}
if(flag)
{
System.out.println(x[i]);
i++;
}
}
break;
}
case 2:{
int j=0;
boolean flag;
while(j<n)
{
int x1=(int) (Math.random()*100);
int y1=(int) (Math.random()*100);
int x2=(int) (Math.random()*100);
int y2=(int) (Math.random()*100);
int z=(int) (Math.random()*2);
y[j]=new String();
if(x1<y1||x2<y2)
{
if(z==0)
y[j]="("+x1+"/"+y1+")+"+"("+x2+"/"+y2+")=";
if(z==1)
y[j]="("+x1+"/"+y1+")-"+"("+x2+"/"+y2+")=";
flag=true;
for(int l=0;l<j;l++)
{
if(y[l].equals(y[j]))
{
flag=false;
break;
}
}
if(flag)
{
System.out.println(y[j]);
j++;
}
}
break;
}
}
}
}
}
public static void kuohao(){
}
public static void fanwei(){
System.out.println("请输入定制的题数:");//定制练习量
Scanner scan=new Scanner(System.in);
int n=scan.nextInt();
System.out.println("请输入数值范围:");//数值范围
Scanner scan2=new Scanner(System.in);
int t=scan2.nextInt();
System.out.println("1、整数练习");//选择题型
System.out.println("2、分数练习");
System.out.println("请选择:");
Scanner scan1=new Scanner(System.in);
int s=scan1.nextInt();
String [] x=new String[n];
String [] y=new String[n];
switch(s){
case 1:{
int i=0;
boolean flag;
while(i<n)
{
int a=(int)(Math.random()*t);
int b=(int)(Math.random()*t);
int c=(int)(Math.random()*4);
x[i]=new String();
if(c==0)
x[i]=a+"+"+b+"=";
if(c==1)
x[i]=a+"-"+b+"=";
if(c==2)
x[i]=a+"*"+b+"=";
else
x[i]=a+"/"+b+"=";
flag=true;
for(int k=0;k<i;k++)
{
if(x[k].equals(x[i]))
{
flag=false;
break;
}
}
if(flag)
{
System.out.println(x[i]);
i++;
}
}
break;
}
case 2:{
int j=0;
boolean flag;
while(j<n)
{
int x1=(int) (Math.random()*t);
int y1=(int) (Math.random()*t);
int x2=(int) (Math.random()*t);
int y2=(int) (Math.random()*t);
int z=(int) (Math.random()*4);
y[j]=new String();
if(x1<y1||x2<y2)
{
if(z==0)
y[j]="("+x1+"/"+y1+")+"+"("+x2+"/"+y2+")=";
if(z==1)
y[j]="("+x1+"/"+y1+")-"+"("+x2+"/"+y2+")=";
if(z==2)
y[j]="("+x1+"/"+y1+")*"+"("+x2+"/"+y2+")=";
if(z==3)
y[j]="("+x1+"/"+y1+")/"+"("+x2+"/"+y2+")=";
flag=true;
for(int l=0;l<j;l++)
{
if(y[l].equals(y[j]))
{
flag=false;
break;
}
}
if(flag)
{
System.out.println(y[j]);
j++;
}
}
break;
}
}
}
}
public static void fushu(){
System.out.println("1、加减无负数");
System.out.println("2、加减有负数");
Scanner scann=new Scanner(System.in);
int sca=scann.nextInt();
switch(sca)
{
case 1:{
dingzhi();
}
case 2:{
System.out.println("请输入定制的题数:");//定制练习量
Scanner scan=new Scanner(System.in);
int n=scan.nextInt();
System.out.println("1、整数练习");//选择题型
System.out.println("2、分数练习");
System.out.println("请选择:");
Scanner scan1=new Scanner(System.in);
int s=scan1.nextInt();
String [] x=new String[n+1];
String [] y=new String[n+1];
switch(s){
case 1:{
int i=0;
boolean flag;
while(i<=n)
{
int a=(int)(Math.random()*100);
int b=(int)(Math.random()*100);
int c=(int)(Math.random()*4);
int d=(int)(Math.random()*2);
int e=(int)(Math.random()*2);
int q=0;
if(d==0)
q=1;
if(d==1)
q=-1;
int p=0;
if(e==0)
p=1;
if(e==1)
p=-1;
x[i]=new String();
if(c==0&&d==0&&e==0)
x[i]=a*q+"+"+b*p+"=";
if(c==0&&d==0&&e==1)
x[i]=a*q+"+"+"("+b*p+")"+"=";
if(c==0&&d==1&&e==0)
x[i]="("+a*q+")"+"+"+b*p+"=";
if(c==0&&d==1&&e==1)
x[i]="("+a*q+")"+"+"+"("+b*p+")"+"=";
if(c==0&&d==0&&e==0)
x[i]=a*q+"-"+b*p+"=";
if(c==0&&d==0&&e==1)
x[i]=a*q+"-"+"("+b*p+")"+"=";
if(c==0&&d==1&&e==0)
x[i]="("+a*q+")"+"-"+b*p+"=";
if(c==0&&d==1&&e==1)
x[i]="("+a*q+")"+"-"+"("+b*p+")"+"=";
if(c==2)
x[i]=a+"*"+b+"=";
if(c==3)
x[i]=a+"/"+b+"=";
flag=true;
for(int k=0;k<i;k++)
{
if(x[k].equals(x[i]))
{
flag=false;
break;
}
}
if(flag)
{
System.out.println(x[i]);
i++;
}
}
break;
}
case 2:{
int j=0;
boolean flag;
while(j<n)
{
int x1=(int) (Math.random()*100);
int y1=(int) (Math.random()*100);
int x2=(int) (Math.random()*100);
int y2=(int) (Math.random()*100);
int z=(int) (Math.random()*4);
y[j]=new String();
if(x1<y1||x2<y2)
{
if(z==0)
y[j]="("+x1+"/"+y1+")+"+"("+x2+"/"+y2+")=";
if(z==1)
y[j]="("+x1+"/"+y1+")-"+"("+x2+"/"+y2+")=";
if(z==2)
y[j]="("+x1+"/"+y1+")*"+"("+x2+"/"+y2+")=";
if(z==3)
y[j]="("+x1+"/"+y1+")/"+"("+x2+"/"+y2+")=";
flag=true;
for(int l=0;l<j;l++)
{
if(y[l].equals(y[j]))
{
flag=false;
break;
}
}
if(flag)
{
System.out.println(y[j]);
j++;
}
}
break;
}
}
}
}
}
}
public static void yushu(){
System.out.println("1、除法有余数");
System.out.println("2、除法无余数");
Scanner scann=new Scanner(System.in);
int sca=scann.nextInt();
if(sca==1)
{
System.out.println("请输入定制的题数:");//定制练习量
Scanner scan=new Scanner(System.in);
int n=scan.nextInt();
String [] x=new String[n];
int i=0;
boolean flag;
while(i<n)
{
int a=(int)(Math.random()*100);
int b=(int)(Math.random()*100);
int c=(int)(Math.random()*4);
x[i]=new String();
if(c==0)
x[i]=a+"+"+b+"=";
if(c==1)
x[i]=a+"-"+b+"=";
if(c==2)
x[i]=a+"*"+b+"=";
if(c==3&&a%b!=0)
x[i]=a+"/"+b+"=";
if(c==3&&a%b==0)
i--;
flag=true;
for(int k=0;k<i;k++)
{
if(x[k].equals(x[i]))
{
flag=false;
break;
}
}
if(flag)
{
System.out.println(x[i]);
i++;
}
}
}
if(sca==2)
{
System.out.println("请输入定制的题数:");//定制练习量
Scanner scan=new Scanner(System.in);
int n=scan.nextInt();
String [] x=new String[n];
int i=0;
boolean flag;
while(i<n)
{
int a=(int)(Math.random()*100);
int b=(int)(Math.random()*100);
int c=(int)(Math.random()*4);
x[i]=new String();
if(c==0)
x[i]=a+"+"+b+"=";
if(c==1)
x[i]=a+"-"+b+"=";
if(c==2)
x[i]=a+"*"+b+"=";
if(c==3&&a%b==0)
x[i]=a+"/"+b+"=";
if(c==3&&a%b!=0)
i--;
flag=true;
for(int k=0;k<i;k++)
{
if(x[k].equals(x[i]))
{
flag=false;
break;
}
}
if(flag)
{
System.out.println(x[i]);
i++;
}
}
}
}
}
截图: