Java教程

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

本文主要是介绍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);
}

}

这篇关于java课堂测试2第一阶段:方法运用的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!