Java黑皮书课后题第5章:5.2(重复加法)程序清单5-4产生了5个随机减法问题。改写该程序,使它产生10个随机加法问题,加数时两个1到15之间的整数。显示正确答案的个数和完成测试的时间
生活随笔
收集整理的這篇文章主要介紹了
Java黑皮书课后题第5章:5.2(重复加法)程序清单5-4产生了5个随机减法问题。改写该程序,使它产生10个随机加法问题,加数时两个1到15之间的整数。显示正确答案的个数和完成测试的时间
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
5.2(重復加法)程序清單5-4產生了5個隨機減法問題。改寫該程序,使它產生10個隨機加法問題,加數時兩個1到15之間的整數。顯示正確答案的個數和完成測試的時間
- 題目
- 題目概述
- 程序清單5-4
- 破題
- 代碼
題目
題目概述
5.2(重復加法)程序清單5-4產生了5個隨機減法問題。改寫該程序,使它產生10個隨機加法問題,加數時兩個1到15之間的整數。顯示正確答案的個數和完成測試的時間
程序清單5-4
import java.util.Scanner;public class qingdan {public static void main(String[] args) {final int NUMBER_OF_QUESTIONS = 5; // Number of questionsint correctCount = 0; // Count the number of correct answersint count = 0; // Count the number of questionslong startTime = System.currentTimeMillis();String output = " "; // output string is initially emptyScanner input = new Scanner(System.in);while(count < NUMBER_OF_QUESTIONS){// 1.Generate two random single-digit integersint number1 = (int) (Math.random() * 10);int number2 = (int) (Math.random() * 10);// 2. If number1 < number2, swap number1 with number2if(number1 < number2){int temp = number1;number1 = number2;number2 = temp;}// 3. Prompt the student to answer "What is number1 - number2?"System.out.println("What is " + number1 + " - " + number2 + "?");int answer = input.nextInt();// 4. Grade the answer and display the resultif(number1 - number2 == answer){System.out.println("You are correct!");correctCount++; // Increase the correct answer count}elseSystem.out.println("Your answer is wrong.\n" + number1+ " - " + number2 + "? ");// Increase the question countcount++;output += "\n" + number1 + "-" + number2 + "=" + answer +((number1 - number2 == answer) ? " correct" : " wrong");}long endTime = System.currentTimeMillis();long testTime = endTime - startTime;System.out.println("Correct count is " + correctCount +"\nTest time is " + testTime / 1000 + " seconds\n" + output);} }破題
代碼
import java.util.Scanner;public class Test5_2 {public static void main(String[] args) {final int NUMBER_OF_QUESTIONS = 10; // Number of questionsint correctCount = 0; // Count the number of correct answersint count = 0; // Count the number of questionslong startTime = System.currentTimeMillis();String output = " "; // output string is initially emptyScanner input = new Scanner(System.in);while(count < NUMBER_OF_QUESTIONS){// 1.Generate two random single-digit integersint number1 = (int) (Math.random() * 15 + 1);int number2 = (int) (Math.random() * 15 + 1);// 3. Prompt the student to answer "What is number1 - number2?"System.out.println("What is " + number1 + " + " + number2 + "?");int answer = input.nextInt();// 4. Grade the answer and display the resultif(number1 + number2 == answer){System.out.println("You are correct!");correctCount++; // Increase the correct answer count}elseSystem.out.println("Your answer is wrong.\n" + number1+ " + " + number2 + "? ");// Increase the question countcount++;output += "\n" + number1 + "+" + number2 + "=" + answer +((number1 + number2 == answer) ? " correct" : " wrong");}long endTime = System.currentTimeMillis();long testTime = endTime - startTime;System.out.println("Correct count is " + correctCount +"\nTest time is " + testTime / 1000 + " seconds\n" + output);} }總結
以上是生活随笔為你收集整理的Java黑皮书课后题第5章:5.2(重复加法)程序清单5-4产生了5个随机减法问题。改写该程序,使它产生10个随机加法问题,加数时两个1到15之间的整数。显示正确答案的个数和完成测试的时间的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Java黑皮书课后题第5章:*5.1(统
- 下一篇: Java黑皮书课后题第5章:5.4(将英