Java隨機數應用&交互輸入比較"以乘法訓練實驗爲例"

Q21.This program helps you practice multiplication
by asking you random multiplication questions
with numbers ranging from 1 to 30
and counting how many you solve correctly.
14 * 8 = 112
Correct!
5 * 12 = 60
Correct!
8 * 3 = 24
Correct!
5 * 5 = 25
Correct!
20 * 14 = 280
Correct!
19 * 14 = 256
Incorrect; the answer was 266
You solved 5 correctly.
Q21. 本程序幫助我們練習乘法,並計算我們算對了多少次

import java.util.Scanner;
import java.util.*;
public class Q20 {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		
		Random rand = new Random();
		int randomnumber1 = rand.nextInt(30)+1;
		int randomnumber2 = rand.nextInt(30)+1;;
		tutor(randomnumber1,randomnumber2);
	
	}
	public static void tutor(int num1,int num2){
		int tryout=0;
		Random rand = new Random();
		int input;
		Scanner console = new Scanner (System.in);
		System.out.print(num1+" * "+num2+" = ");
		input = console.nextInt();
		
		while(input==(num1*num2)) {
			System.out.println("Correct!");
			num1 = rand.nextInt(30)+1;
			num2 = rand.nextInt(30)+1;
			
			System.out.print(num1+" * "+num2+" = ");
			input = console.nextInt();
			tryout++;
		}
		System.out.println("Incorrect;the answer was "+ num1*num2);
		System.out.println("You solved "+tryout+" correctly.");
}
}

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