2015/3/6软件工程概论当堂作业

实验要求:编写一段代码,执行结果输出30道四则运算题目,要考虑小学二年级学生的水平

实验思路:利用 random方法来生成随机数,定义各个数范围;用switch随机选择一个运算符,再编辑数和符号的输出顺序

代码如下:

import java.util.*;

public class Test {

 public static void main(String[] args) {

                // TODO Auto-generated method stub

                for (int i = 0; i < 30; i++) {

                     Random r = new Random();

                     int a = r.nextInt(100);

                     int b = r.nextInt(100);

                     int c = r.nextInt(4);

                     char d = ' ';

                     switch (c) {

                     case 0:

                               d = '+';

                               break;

                     case 1:

                               d = '-';

                               break;

                    case 2:

                              d = '×';

                              break;

                    case 3:

                              d = '÷';

                              if(b==0){

                                           i--;

                                          continue;

                              }

                              break;

                   default:

                              d = ' ';

                   }

                   System.out.println(i + 1 + "." + a + d + b);

               }  

        }

}

 

posted @ 2015-03-08 16:17  阳光少年郎  阅读(95)  评论(0编辑  收藏  举报