如何用java實現十道加法求和,算出答對幾道,答錯幾道!

來源:互聯網
上載者:User

標籤:main   rgs   system   技術分享   else   實現   ++   log   oid   

  public static void main(String[] args) {
  Scanner input=new Scanner(System.in);      //需要匯入import java.util.Random;包
  Random a1=new Random(); //第一偽隨機數對象
  Random a2=new Random(); //第二個偽隨機數對象
  int bool=0;//定義電腦  --第幾道題
  int true1=0;//定義答對了幾道
  int false1=0; //定義答錯了幾道
  while (bool<10) {
   System.out.println("第"+(bool+1)+"道題:");
   int one=a1.nextInt(100); //接收隨機數,限制隨機數在100以內
   int two=a2.nextInt(100);//接收隨機數,限制隨機數在100以內
     System.out.print(one+"+"+two+"=");
     int sum=input.nextInt();  //接收輸入的和   //需要匯入import java.util.Scanner; 包
     //判斷
     if(sum==(one+two)) 
     {
      System.out.println("答對了!");
      true1++;  //計算機,答對+1道
     }else{
      System.out.println("答錯了!");
      false1++;//計算機,答錯 +1道
     }
     bool++;
  }
  System.out.println("您答對了:"+true1+"道,答錯了:"+false1);
 }
  

如何用java實現十道加法求和,算出答對幾道,答錯幾道!

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.