设计思路:定义四个数组,其中两个用于算数,一个用于随机,根据其结果输出算法,最后一个用于随机整数运算还是分数运算

package 课堂;
import java.util.Random;

public class sizeyusuan
{
    static void fuhao(int value)
    {
        if(value == 0)
        {
            System.out.print(" + ");
        }
        if(value == 1)
        {
            System.out.print(" - ");
        }
        if(value == 2)
        {
            System.out.print(" * ");
        }
        if(value == 3)
        {
            System.out.print(" / ");
        }
    }
    public static void main(String args[])
    {
        int []Array = new int[30];int []Array1 = new int[30];
        int []judge = new int[30];int []Array2 = new int[30];
        for(int i = 0;i < 30;i++)
        {
            Array[i] = new Random().nextInt(100);
            Array1[i] = new Random().nextInt(100);
            Array2[i] = new Random().nextInt(4);
            judge[i] = new Random().nextInt(2);
        }
        for(int i = 0;i < 30;i++)
        {
            if(judge[i] == 0)
            {
                while(Array2[i] == 3 && Array1[i] == 0)
                {
                    Array1[i] = new Random().nextInt(100);
                    if(Array1[i] != 0)
                    {
                        break;
                    }
                }
                System.out.print(Array[i]);
                fuhao(Array2[i]);
                System.out.println(Array1[i] + " = ");
            }
            if(judge[i] == 1)
            {
                int a = new Random().nextInt(100);
                int b = new Random().nextInt(100);
                    
                while(Array2[i] == 3)
                {
                    if(Array1[i] == 0)
                    {
                        Array1[i] = new Random().nextInt(100);
                    }
                    if(b == 0)
                    {
                        b = new Random().nextInt(100);
                    }
                    if(Array[i] != 0 && b != 0)
                    {
                        break;
                    }
                }
                
                if(Array[i] > Array1[i])
                {
                    int t = Array[i];
                    Array[i] = Array1[i];
                    Array[i] = t;
                }
                if(a > b)
                {
                    int t = a;
                    a = b;
                    b = t;
                }
                
                System.out.print("(" + Array[i] + "/" + Array1[i] + ")");
                fuhao(Array2[i]);
                System.out.println("(" + a + "/" + b + ") = ");
            }
        }
    }
}



课上未完成的原因。
  没有优化,数组超出范围。

 

posted on 2016-03-11 16:10  消失。  阅读(161)  评论(1编辑  收藏  举报