随机生成N道四组运算题
Posted 022414ls
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了随机生成N道四组运算题相关的知识,希望对你有一定的参考价值。
要求:
可随意生成指定数量的题目;可控制每一行有多少个题目;不能有重复;(利用数组查重)
只有加减乘除四则运算,其中乘法要求结果不超过两位数,减法得数不能是负数(换位置),除法结果是整数;
package lianxi1;
import java.util.*;
public class Operation1
static int i = 0;
static int j = 0;
static boolean trag = true;
static public int getNumbera()
int a = (int)(1+Math.random()*100);
return a;
static public int getNumberb()
int b = (int)(1+Math.random()*100);
return b;
static public int getNumberc()
int c = (int)(1+Math.random()*4);
return c;
import java.util.*;
public class Operation1
static int i = 0;
static int j = 0;
static boolean trag = true;
static public int getNumbera()
int a = (int)(1+Math.random()*100);
return a;
static public int getNumberb()
int b = (int)(1+Math.random()*100);
return b;
static public int getNumberc()
int c = (int)(1+Math.random()*4);
return c;
public static void main(String[] args)
// TODO Auto-generated method stub
Scanner sc = new Scanner(System.in);
System.out.println("请输入需要的题目数量:");
int n = sc.nextInt();
System.out.println("请输入一行的题目数量:");
int line = sc.nextInt();
int x[] = new int[100000];
int y[] = new int[100000];
int z[] = new int[100000];
int temp;
for(int i=0;i<n;i++)
for(int j=0;j<line;j++,i++)
char f = ‘0‘;
x[i] = getNumbera();
y[i] = getNumberb();
z[i] = getNumberc();
int k = 0;
while(x[k]==x[i]&&y[k]==y[i]&&z[k]==z[i]&&k<=i)
x[i] = getNumbera();
y[i] = getNumberb();
z[i] = getNumberc();
k++;
switch(z[i])
case 1:
f = ‘+‘;
break;
case 2:
f = ‘-‘;
if(x[i]<y[i])
temp = x[i];
x[i] = y[i];
y[i] = temp;
break;
case 3:
f = ‘*‘;
if(x[i]*y[i]>100)
x[i]=x[i]/10;
if(x[i]*y[i]>100)
y[i] = y[i]/10;
break;
case 4:
f = ‘/‘;
if(x[i]%y[i]!=0&&x[i]>y[i])
x[i] = x[i] - (x[i]%y[i]);
break;
System.out.print(i+". "+x[i]+f+y[i]+"="+" ");
System.out.println();
// TODO Auto-generated method stub
Scanner sc = new Scanner(System.in);
System.out.println("请输入需要的题目数量:");
int n = sc.nextInt();
System.out.println("请输入一行的题目数量:");
int line = sc.nextInt();
int x[] = new int[100000];
int y[] = new int[100000];
int z[] = new int[100000];
int temp;
for(int i=0;i<n;i++)
for(int j=0;j<line;j++,i++)
char f = ‘0‘;
x[i] = getNumbera();
y[i] = getNumberb();
z[i] = getNumberc();
int k = 0;
while(x[k]==x[i]&&y[k]==y[i]&&z[k]==z[i]&&k<=i)
x[i] = getNumbera();
y[i] = getNumberb();
z[i] = getNumberc();
k++;
switch(z[i])
case 1:
f = ‘+‘;
break;
case 2:
f = ‘-‘;
if(x[i]<y[i])
temp = x[i];
x[i] = y[i];
y[i] = temp;
break;
case 3:
f = ‘*‘;
if(x[i]*y[i]>100)
x[i]=x[i]/10;
if(x[i]*y[i]>100)
y[i] = y[i]/10;
break;
case 4:
f = ‘/‘;
if(x[i]%y[i]!=0&&x[i]>y[i])
x[i] = x[i] - (x[i]%y[i]);
break;
System.out.print(i+". "+x[i]+f+y[i]+"="+" ");
System.out.println();
以上是关于随机生成N道四组运算题的主要内容,如果未能解决你的问题,请参考以下文章