自動生成小學四則運算程式

康取發表於2015-03-26

 

import java.lang.*;
import java.util.Scanner;

public class Kq {

    /**
     * @param args
     */
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        int a, b, c;
        int systemanswer = 0;
        int useranswer = 0;
        int rightnumber = 0;
        char op = 0;
        System.out.println("歡迎使用自動生成小學四則運算程式,本程式每一次出十道題,使用者做對6題或6題以上即退出程式");
        do {
            for (int i = 1; i <= 10; i++) {
                a = 1 + (int) (Math.random() * 10);
                b = 1 + (int) (Math.random() * 10);
                c = 1 + (int) (Math.random() * 4);
                switch (c) {
                case 1:
                    op = '+';
                    systemanswer = a + b;
                    break;
                case 2:
                    op = '-';
                    systemanswer = a - b;
                    break;
                case 3:
                    op = '*';
                    systemanswer = a * b;
                    break;
                case 4:
                    op = '/';
                    systemanswer = a / b;
                    break;
                default:
                    System.out.printf("無效");
                }
                System.out.printf("%d%c%d=?\n", a, op, b);
                Scanner in = new Scanner(System.in);
                useranswer = in.nextInt();
                if (useranswer == systemanswer) {
                    System.out.printf("right!\n");
                    rightnumber++;
                } else {
                    System.out.printf("wrong!\n");
                }
            }
            System.out.printf("總分%d\n", rightnumber * 10);
            System.out.printf("百分比%d%%\n", rightnumber * 10);
            if (rightnumber * 10 < 60) {
                System.out.printf("再來一遍\n");
                rightnumber = 0;
            }
        } while (rightnumber * 10 < 60);

    }

}

 

相關文章