J2SE題目—隨機加法計算器

需要的功能:
1.程序依次出十道100以內的加法題目
2.用戶依次輸入每道題答案(程序出一道題,用戶答一道題)
3.用戶每答一道題,程序給出結果提示
4.用戶輸入-1提前退出程序

import java.util.Scanner;

public class Calculator {

    /*
     * > 需要的功能:
     * >1.程序依次出十道100以內的加法題目
     * >2.用戶依次輸入每道題答案(程序出一道題,用戶答一道題)
     * >3.用戶每答一道題,程序給出結果提示
     * >4.用戶輸入-1提前退出程序
     */
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int score = 0;
        for(int count = 1;count <= 10;count++) {
            int random1 = (int) (Math.random()*1000);
            int random2 = (int) (Math.random()*1000);
            System.out.println("第"+count+"題,請問:"+random1+"+"+random2+"=?");
            int userAns = (int) scanner.nextDouble();
            if (userAns==-1) {
                System.out.println("再見");
                break;
            }
            else if (userAns==random1+random2) {
                System.out.println("答對了");
                score++;
            }
            else System.out.println("打錯了,正確答案是:"+(random1+random2));

        }
        scanner.close();
        System.out.println("最終得分"+score);
    }

}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章