Untitled

mail@pastecode.io avatar
unknown
plain_text
2 years ago
4.5 kB
1
Indexable
Never
import java.util.Locale;
import java.util.Scanner;

public class Activity4{

	public static void main(String[] args) {
		final int NUMBER_OF_QUESTIONS = 5; // Number of questions
		int correctCount = 0; // Count the number of correct answers
		int count = 0; // Count the number of questions
		long startTime = System.currentTimeMillis();
		String output = ""; // output string is initially empty
		Scanner input = new Scanner(System.in);
		input.useLocale(Locale.US);

		while (count < NUMBER_OF_QUESTIONS) {
	
			int number1 = (int) (Math.random() * 10);
			int number2 = (int) (Math.random() * 10);


			int min = 0;
			int max = 1000;


			int randomNumber1 = min + (int) (Math.random() * ((max - min) + 1));
			int randomNumber2 = min + (int) (Math.random() * ((max - min) + 1));

		
			if (randomNumber1 < randomNumber2) {
				int temp = randomNumber1;
				randomNumber1 = randomNumber2;
				randomNumber2 = temp;

			}

			

			int randomDigit = (int) (Math.random() * 5);

			String operation = switch (randomDigit) {
			case 0:
				yield operation = "+";

			case 1:
				yield operation = "-";

			case 2:
				yield operation = "*";
			case 3:
				yield operation = "/";

			default:
				yield operation = "%";
			};


			if ((operation != "/" || operation != "%") && randomNumber2 != 0) {
				System.out.print("What is " + randomNumber1 + operation + randomNumber2 + "? ");
			} else {
				continue;
			}

			int answer;
			double answerDouble;

			// 4. Grade the answer and display the result
			switch (operation) {
			case "-":

				answer = input.nextInt();
				if (randomNumber1 - randomNumber2 == answer) {
					System.out.println("You are correct!");
					correctCount++;
				} else
					System.out.println("Your answer is wrong.\n" + randomNumber1 + operation + randomNumber2
							+ " should be " + (randomNumber1 - randomNumber2));
				// Increase the count
				count++;
				output += "\n" + randomNumber1 + operation + randomNumber2 + "=" + answer
						+ ((randomNumber1 - randomNumber2 == answer) ? " correct" : " wrong");

				break;

			case "+":
				answer = input.nextInt();
				if (randomNumber1 + randomNumber2 == answer) {
					System.out.println("You are correct!");
					correctCount++;
				} else
					System.out.println("Your answer is wrong.\n" + randomNumber1 + operation + randomNumber2
							+ " should be " + (randomNumber1 + randomNumber2));
				// Increase the count
				count++;
				output += "\n" + randomNumber1 + operation + randomNumber2 + "=" + answer
						+ ((randomNumber1 + randomNumber2 == answer) ? " correct" : " wrong");

				break;

			case "*":
				answer = input.nextInt();
				if (randomNumber1 * randomNumber2 == answer) {
					System.out.println("You are correct!");
					correctCount++;
				} else
					System.out.println("Your answer is wrong.\n" + randomNumber1 + operation + randomNumber2
							+ " should be " + (randomNumber1 * randomNumber2));
				// Increase the count
				count++;
				output += "\n" + randomNumber1 + operation + randomNumber2 + "=" + answer
						+ ((randomNumber1 * randomNumber2 == answer) ? " correct" : " wrong");
				break;

			case "/":
				answerDouble = input.nextDouble();

				if ((double) randomNumber1 / randomNumber2 == answerDouble) {
					System.out.println("You are correct!");
					correctCount++;
				} else
					System.out.println("Your answer is wrong.\n" + randomNumber1 + operation + randomNumber2
							+ " should be " + ((double) randomNumber1 / randomNumber2));

				// Increase the count
				count++;
				output += "\n" + randomNumber1 + operation + randomNumber2 + "=" + answerDouble
						+ (((double) randomNumber1 / randomNumber2 == answerDouble) ? " correct" : " wrong");

				break;

			default:
				answer = input.nextInt();
				if (randomNumber1 % randomNumber2 == answer) {
					System.out.println("You are correct!");
					correctCount++;
				} else
					System.out.println("Your answer is wrong.\n" + randomNumber1 + operation + randomNumber2
							+ " should be " + (randomNumber1 % randomNumber2));
				// Increase the count
				count++;
				output += "\n" + randomNumber1 + operation + randomNumber2 + "=" + answer
						+ ((randomNumber1 % randomNumber2 == answer) ? " correct" : " wrong");

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

	}
}