返回列表 發帖

Date 類別 (二)

本帖最後由 tonyh 於 2014-10-25 15:06 編輯

設計一程式, 讓使用者計算四個範圍介於11~99之隨機亂數的和.
判斷作答是否正確, 並告訴使用者該題花了多久的時間思考. 參考畫面如下:



本帖隱藏的內容需要回復才可以瀏覽
附件: 您需要登錄才可以下載或查看附件。沒有帳號?註冊

  1. import java.util.Date;
  2. public class ch51
  3. {
  4.     public static void main(String args[])
  5.     {
  6.         Scanner s=new Scanner(System.in);
  7.         int a,b,c,d;
  8.         int answer;
  9.         for(int i=0;i<=5;i++)
  10.         {
  11.                 a=(int)(Math.random()*89+11);
  12.                 b=(int)(Math.random()*89+11);
  13.                 c=(int)(Math.random()*89+11);
  14.                 d=(int)(Math.random()*89+11);
  15.                 System.out.print(a+"+"+b+"+"+c+"+"+d+"= ");
  16.                 long t1=new Date().getTime();
  17.                 answer=s.nextInt();
  18.                 long t1=new Date().getTime();
  19.                 long timePassed=t2-t1;
  20.                 if(answer==(a+b+c+d))
  21.                      System.out.print("答對了");
  22.                 else
  23.                      System.out.print("答錯了!答案是"+(a+b+c+d));
  24.                      System.out.println("你剛花了"+timePassed+"毫秒");
  25.                      System.out.println();
  26.         }
  27.     }
  28. }
複製代碼

TOP

  1. import java.util.Scanner;
  2. import java.util.Date;
  3. public class ch52
  4. {
  5.     public static void main(String args[])
  6.     {
  7.         Scanner s=new Scanner(System.in);
  8.         int a, b, c, d;
  9.         int answer;
  10.         for(int i=1; i<=5; i++)
  11.         {
  12.             a=(int)(Math.random()*89+11);
  13.             b=(int)(Math.random()*89+11);
  14.             c=(int)(Math.random()*89+11);
  15.             d=(int)(Math.random()*89+11);
  16.             System.out.print(a+"+"+b+"+"+c+"+"+d+"= ");
  17.             long t1=new Date().getTime();
  18.             answer=s.nextInt();
  19.             long t2=new Date().getTime();
  20.             answer=timepassed=t2-t1;
  21.             if(answer==(a+b+c+d))
  22.                 System.out.print("答對了!!!")
  23.             else
  24.                 System.out.print("答錯了!!! 正確答案時"+(a+b+c+d)+". ")
  25.             System.out.println("你剛花了"+timepassed+"毫秒思考")
  26.             System.out.print();   
  27.         }
  28.     }
  29. }
複製代碼

TOP

  1. import java.util.Scanner;
  2. import java.util.Date;
  3. public class ch51
  4. {
  5.     public static void main(String args[])
  6.     {
  7.         Scanner s=new Scanner(System.in);
  8.         int a, b, c, d;
  9.         int answer;
  10.         for(int i=0; i<=5; i++)
  11.         {
  12.             a=(int)(Math.random()*89+11);
  13.             b=(int)(Math.random()*89+11);
  14.             c=(int)(Math.random()*89+11);
  15.             d=(int)(Math.random()*89+11);
  16.             System.out.print(a+"+"+b+"+"+c+"+"+d+"=");
  17.             long t1=new Date().getTime();
  18.             answer=s.nextInt();
  19.             long t1=new Date().getTime();
  20.             long timePassed=t2-t1;
  21.             if(answer==(a+b+c+d))
  22.                System.out.print("答對了!");
  23.             else
  24.                System.out.print("答錯了!正確答案是"+(a+b+c+d)+". ");

  25.                System.out.println("你剛花了"+timePassed+"毫秒思考!");
  26.                System.out.println();
  27.     }
  28. }
複製代碼

TOP

  1. import java.util.Scanner;
  2. import java.util.Date;
  3. public class ch51
  4. {
  5.     public static void main(String args[])
  6.     {
  7.         Scanner s=new Scanner(System.in);
  8.         int a, b, c, d;
  9.         int answer;
  10.         for(int i=0; i<=5; i++)
  11.         {
  12.             a=(int)(Math.random()*89+11);
  13.             b=(int)(Math.random()*89+11);
  14.             c=(int)(Math.random()*89+11);
  15.             d=(int)(Math.random()*89+11);
  16.             System.out.print(a+"+"+b+"+"+c+"+"+d+"=");
  17.             long t1=new Date().getTime();
  18.             answer=s.nextInt();
  19.             long t1=new Date().getTime();
  20.             long timePassed=t2-t1;
  21.             if(answer==(a+b+c+d))
  22.                System.out.print("答對了!");
  23.             else
  24.                System.out.print("答錯了!正確答案是"+(a+b+c+d)+". ");

  25.                System.out.println("你剛花了"+timePassed+"毫秒思考!");
  26.                System.out.println();
  27.     }
  28. }
複製代碼

TOP

返回列表