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);
}
}
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· AI与.NET技术实操系列:基于图像分类模型对图像进行分类
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 25岁的心里话
· 闲置电脑爆改个人服务器(超详细) #公网映射 #Vmware虚拟网络编辑器
· 零经验选手,Compose 一天开发一款小游戏!
· 因为Apifox不支持离线,我果断选择了Apipost!
· 通过 API 将Deepseek响应流式内容输出到前端