首页 > 编程语言 > 详细

Java简单减法测试并计做题时间

时间:2020-03-15 15:25:04      阅读:74      评论:0      收藏:0      [点我收藏+]

描述

设计5道10以内的随机减法测试题,要求回答完立刻判断对错,如果错误需要显示正确答案,并统计正确的题数和答题和时间,最后显示做题情况。

输入

输入所给减法题的计算答案

输出

输出随机生成的题目;

如果输入的数与答案一致,输出System.out.println("You are correct!");

如果输入的数与答案不一致,则输出System.out.println("Your answer is wrong.\n" +n1 + " - " + n2 + " should be " + (n1 - n2));

答完题后输出答题正确的题数和测试时间;

最后输出做题情况,即题目、输入答案、是否正确,例如:4 - 1 = 2 wrong4 - 4 = 0 correct

难度

较难

输入示例

5
8
3
1
2

输出示例

What is 9 - 4 = ?
5
You are correct!
What is 8 - 0 = ?
8
You are correct!
What is 4 - 1 = ?
3
You are correct!
What is 6 - 5 = ?
1
You are correct!
What is 9 - 8 = ?
2
Your answer is wrong.
9 - 8 should be 1
Correct count is 4
Test time is 8 seconds
9 - 4 = 5 correct
8 - 0 = 8 correct
4 - 1 = 3 correct
6 - 5 = 1 correct
9 - 8 = 2 wrong
import java.util.Scanner;

public class AdditionQuiz {
    public static void main(String[] args) {
        final int NUMBER_OF_QUESTIONS = 5;
        int correctCount = 0;
        int count = 0;
        long starTime = System.currentTimeMillis();//重点
        String output = "";
        Scanner input = new Scanner(System.in);
        while (count< NUMBER_OF_QUESTIONS) {
            //int n1=(int)(System.currentTimeMillis() * 10);
            //int n2=(int)(System.currentTimeMillis() * 10);
            int n1 = (int) (Math.random() * 10);
            int n2 = (int) (Math.random() * 10);

            if (n1 < n2) {
                int t = n1;
                n1 = n2;
                n2 = t;
            }
            System.out.println(
                    "What is " + n1 + " - " + n2 + " = ?"
            );
            int answer = input.nextInt();
            if (n1 - n2 == answer) {
                System.out.println("You are correct!");
                correctCount++;
            } else
                System.out.println("Your answer is wrong.\n" +
                        n1 + " - " + n2 + " should be " + (n1 - n2));
            count++;
            output += "\n" + n1 + " - " + n2 + " = " + answer +
                    ((n1 - n2 == answer) ? " correct" : " wrong");
        }

        long endTime = System.currentTimeMillis();
        long testTime = endTime - starTime;
        System.out.println("Correct count is " + correctCount +
                "\nTest time is " + testTime / 1000 + " seconds\n" + output);

        input.close();
    }
}

 

Java简单减法测试并计做题时间

原文:https://www.cnblogs.com/Blogwjl/p/12497375.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!