码迷,mamicode.com
首页 > 其他好文 > 详细

随机生成N道四组运算题

时间:2019-09-22 12:47:23      阅读:96      评论:0      收藏:0      [点我收藏+]

标签:case   bsp   rgs   ati   can   temp   for   ann   har   

要求:

可随意生成指定数量的题目;可控制每一行有多少个题目;不能有重复;(利用数组查重)

只有加减乘除四则运算,其中乘法要求结果不超过两位数,减法得数不能是负数(换位置),除法结果是整数;

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;
 }
 
 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();
  }
 }
}

 

随机生成N道四组运算题

标签:case   bsp   rgs   ati   can   temp   for   ann   har   

原文地址:https://www.cnblogs.com/022414ls/p/11566852.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!