java课堂测试2第一阶段:方法运用

package test2;

import java.util.*;

public class Test2 {

public static int generateRandom(int fanwei) { //生成随机数
Random random=new Random();
int R=random.nextInt(fanwei);
return R;
}
public static String operator(int fanwei) { //生成随机运算符
Random random=new Random();
int r=random.nextInt(fanwei);
String[] a=new String[fanwei];
for(int i=0;i<fanwei;i++)
{
switch(i%4) {
case 0: a[i]="+";break;
case 1: a[i]="-";break;
case 2: a[i]="*";break;
case 3: a[i]="/";break;
}
}
return a[r];

}

public static String[][] deposit(int geshu,int caozuo,int fanwei) { //将题存放在字符串数组中
int i,j,k;
String[][] ti=new String[geshu][caozuo*2];
for(i=0;i<geshu;i++)
{
for(j=0;j<caozuo*2-1;j+=2)
{
k=generateRandom(fanwei);
ti[i][j]=String.valueOf(k);
}
for(j=1;j<caozuo*2-1;j+=2)
{
ti[i][j]=operator(fanwei);
}
ti[i][caozuo*2-1]="=";
}
return ti;


}

public static void put(int geshu,int caozuo,int fanwei) { //输出
int i,j;
String[][] ti=new String[geshu][caozuo*2];
ti=deposit(geshu,caozuo,fanwei);
for(i=0;i<geshu;i++)
{
for(j=0;j<caozuo*2;j++)
{
System.out.print(ti[i][j]);
}
System.out.println();
}
}
public static int repeat(int geshu,int caozuo,int fanwei) { //判断题目是否重复
int i,j,k=1;
String[][] ti=new String[geshu][caozuo*2];
ti=deposit(geshu,caozuo,fanwei);
for(i=0;i<geshu;i++)
{
for(j=0;j<i;j++)
{
if(ti[j].equals(ti[i]))
{
k=0;
break;
}
else continue;
}
if(k==0) break;
else continue;
}
return k;

}

public static void main(String[] args)
{

Scanner s=new Scanner(System.in);

System.out.println("请输入出题个数:");
int geshu=s.nextInt();

System.out.println("请输入操作数的个数:");
int caozuo=s.nextInt();

System.out.println("请输入操作数的取值范围:");
int fanwei=s.nextInt();


deposit(geshu,caozuo,fanwei);
do {
repeat(geshu,caozuo,fanwei);
}while(repeat(geshu,caozuo,fanwei)==0);
put(geshu,caozuo,fanwei);
}

}

上一篇:WPF&Winform版本地图引擎


下一篇:在centos7下安装java8和mysql