• 软件工程个人作业01


    一、设计思想

    利用随机数获取运算的数据和运算的符号,进行加和输出运算。

    二、源程序代码

    import java.util.Random;

    import java.util.Scanner;

     

    public class Yunsuan {

     

           int x;

           int y;

           int z;

           int  sum;

           char c;

           String string;

           String[] s= {"+","-","*","/"};

           public  int getX() {

                  return x;

           }

           public void setX(int x) {

                  this.x = x;

           }

           public int getY() {

                  return y;

           }

           public void setY(int y) {

                  this.y = y;

           }

           public String getC() {

                  Random random = new Random();

                  int n=random.nextInt(4);

                  if(n<=1) {

                         return s[random.nextInt(s.length)];

                  }

                  else {

                         return s[random.nextInt(s.length)];

                  }

           }

           public String getString() {

                  return string;

           }

           public void setString(String string) {

                  this.string = string;

           }    

     

           public void setSum(int sum) {

                  this.sum = sum;

           }

           public int Getsum() {

                  return sum;

           }

           public void random() { //灏佽�鐢熸垚鐨勯殢鏈烘暟

                  Random r=new Random();

                  x=r.nextInt(99);

                  y=r.nextInt(99);

                  z=r.nextInt(4);

                 

                  switch(getC()) {

                  case "+":

                         sum=x+y;

                         break;

                  case "-":

                         sum=x-y;

                         break;

                  case "*":

                         if(x>10||y>10)

                         {

                                random();

                         }

                         else

                         {

                         sum=x*y;

                         }

                         break;

                  case "/":

                         if(y==0) //闄ゆ暟涓�0閲嶆柊璋冪敤

                         {

                                random();

                         }

                         else

                         {

                                sum=x/y;

                         }

                        

                         break;

                                }

                  setX(x);

                  setY(y);

                  setSum(sum);

           }

          

           public void yunsuan(){

                  //random(); //璋冪敤鐢熸垚闅忔満杩愮畻

                         random();

                  /*if(sum<100||sum>0)

                  { //鍜屽ぇ浜�100閲嶆柊鐢熸垚

     

                         random();

                 

                  }

                  else

                  {

                         yunsuan();

                  }*/

                  String a=x+" "+c+" "+y;

                  setString(a);

                  if(c=='/')

                  {

                         System.out.println(x+" "+getC()+" "+y+"="+x+""+c+""+y);

                  }

                  else {

                  System.out.println(x+" "+c+" "+y+"="+sum);

                  }

           }

          

                 

          

          

           public static void main(String[] args) {

                  // TODO Auto-generated method stub

                 

                  Yunsuan yunsuan=new Yunsuan();

                  int n;

                  Scanner in=new Scanner(System.in);

                  n=in.nextInt();

                  for(int i=0;i<n;i++ )

                  {

                  yunsuan.yunsuan();

                  }

           }

     

    }

    三、运行结果

     

     

  • 相关阅读:
    Shell编程学习1-变量的高级用法
    Ubuntu新机配置深度学习环境遇到的问题
    Python细致技巧总结(不断更新)
    图片着色后存储为“JPEG”格式存在明显色差问题解决
    python图片合成视频
    caffe-ssd安装问题解决
    python画图
    python split(),os.path.split()和os.path.splitext()函数用法
    转载:mysql 存储过程
    css实现div框阴影
  • 原文地址:https://www.cnblogs.com/liulala2017/p/8252129.html
Copyright © 2020-2023  润新知