java隨機輸出10計算題

來源:互聯網
上載者:User

標籤:out   判斷   產生   dom   scan   erro   ann   構建   count   

方法1.

public static void main(String[] args){
System.out.println("將開始10次加法測試");
Scanner scanner=new Scanner(System.in);
int count=0;
for(int i=1;i<=10;i++){

int a=(int) (Math.random()*1000)+1;
int b=(int) (Math.random()*1000)+1;
System.out.println("("+i+")."+a+"+"+b+"=?");
System.out.println("請輸入答案輸入-1退出:");
int resullt=scanner.nextInt();
if(resullt==-1){
return;
}
if((a+b)==resullt){
count++;
System.out.println("正確");
}else{
System.out.println("錯誤");
}
}
System.out.println("此測試結束您的得分為"+count*10);

方法二.

public static void main(String[] args) {
// TODO Auto-generated method stub
System.out.println("將開始10次加法測試...");
Scanner scanner=new Scanner(System.in);
//用於記載分數
int score=0;

//構建10次迴圈
for(int i=1;i<=10;i++){
//隨機產生兩個加數
int a=(int) (Math.random()*100);
int b=(int) (Math.random()*100);
int result=a+b;

//輸出需要計算的加法運算式
System.out.println("("+i+")."+a+"+"+b+"=?");
System.out.println("請輸入答案(輸入-1退出):");

int answer=scanner.nextInt();
//判斷對錯
if(answer == -1){
break;
}else if(answer != result){
System.out.println("Error!");
continue;
}else{
score+=10;
System.out.println("Correct!");
//continue;
}

}
scanner.close();
System.out.println("此次測驗結束,你的分數是:"+score);

思考:1.return直接返回下邊程式不再執行,break終止當前迴圈,下邊的程式依然會執行,所以第一程式存在bug。

        2.continue的使用。

        3.變數名的使用。

 

java隨機輸出10計算題

聯繫我們

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