四則運算(三年級)

來源:互聯網
上載者:User

標籤:函數   結果   imp   package   隨機函數   main   除法   system.in   public   

package test;

 

import java.util.Scanner;

import java.util.Random;

public class Main {

  public static void main(String[] args)

  {

         Random n = new Random();//隨機函數

         int[] a = new int[30];//儲存式子序號

         int[] b = new int[30];//儲存第一個數

         int[] c = new int[30];//儲存運算子號對應的數字

         int[] d = new int[30];//儲存第二個數

         int[] e = new int[30];//儲存輸入的結果

         int[] f = new int[30];//儲存正確結果

         for(int i = 1;i <= 30; i++)//出題

         {

                System.out.print(i+". ");

                a[i-1] = i;

                b[i-1] = n.nextInt(100)+1;

                System.out.print(b[i-1]);

                int j = n.nextInt(4) + 1;

                c[i-1] = j;

                switch(j)//隨機出符號

                {

                       case 1:System.out.print("+");break;

                       case 2:System.out.print("-");break;

                       case 3:System.out.print("*");break;

                       case 4:System.out.print("/");break;

                }

                if(j == 2) d[i-1] = n.nextInt(b[i-1])+1;//減法第二個數字不能比第一個大

                else if(j == 4) //除法結果需要為整數

                {

                       int m = 1;

                       int p = 1;

                       while(p > 0)//判斷能否被整除

                       {

                              m = n.nextInt(b[i-1])+1;

                              if(b[i-1]%m == 0) p = 0;

                       }

                       d[i-1] = m;

                }

                else d[i-1] = n.nextInt(100)+1;

                System.out.print(d[i-1]);

                System.out.println("=");

                Scanner scanner = new Scanner(System.in);

                e[i-1] = scanner.nextInt();

         }

         int k = 0;

         System.out.println("正確的題目有:");

         for(int j = 0;j < 30;j++)//輸出正確的題目序號

         {

                switch(c[j])

                {

                       case 1:f[j] = b[j] + d[j];break;

                       case 2:f[j] = b[j] - d[j];break;

                       case 3:f[j] = b[j] * d[j];break;

                       case 4:f[j] = b[j] / d[j];break;

                }

                if(f[j] == e[j]) //判斷結果是否正確

                {

                       System.out.print(a[j]+" ");

                       k++;//計數正確題數

                }

         }

         System.out.println("共正確"+k+"道題!!!");

  }

}

四則運算(三年級)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.