Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package practice.programpractice;
- import java.util.*;
- public class switchmethodQuiz {
- //Author Zunder Jacob A. Pacis
- // 1.3.1
- public static void main(String[] args) {
- Scanner sc = new Scanner(System.in);
- System.out.println("How many items do you want to answer?");
- byte item = sc.nextByte();
- byte i;
- System.out.println("What math operation do you want to solve?\n1 - addition\t\t2 - subtraction\n3 - multiplication\t4 - division");
- byte operator = sc.nextByte();
- int score = 0;
- String correct = "Your answer is correct!";
- String wrong = "Your answer is wrong!, the correct answer is ";
- switch (operator) {
- case 1 -> System.out.println("You have chosen Addition!\n");
- case 2 -> System.out.println("You have chosen Subtraction!\n");
- case 3 -> System.out.println("You have chosen Multiplication!\n");
- case 4 -> System.out.println("You have chosen Division!\n");
- default -> {
- }
- }
- for (i = 1; i <= item; i++) {
- switch (operator) {
- case 1 -> {
- int a = (int) (Math.random() * 11);
- int b = (int) (Math.random() * 11);
- String question = quest(a,b,i,operator);
- System.out.println(question);
- int ans = a+b;
- int input = sc.nextInt();
- if (input==a+b) {
- System.out.println(correct);
- score++;
- }
- else System.out.println(wrong + ans);
- }
- case 2 -> {
- int a = (int) (Math.random() * 11);
- int b = (int) (Math.random() * 11);
- String question = quest(a,b,i,operator);
- System.out.println(question);
- int ans = a-b;
- int input = sc.nextInt();
- if (input==a-b) {
- System.out.println(correct);
- score++;
- }
- else System.out.println(wrong + ans);
- }
- case 3 -> {
- int a = (int) (Math.random() * 11);
- int b = (int) (Math.random() * 11);
- String question = quest(a,b,i,operator);
- System.out.println(question);
- int ans = a*b;
- int input = sc.nextInt();
- if (input==a*b) {
- System.out.println(correct);
- score++;
- }
- else System.out.println(wrong + ans);
- }
- case 4 -> {
- int a = (int) (Math.random() * 11);
- int b = (int) (Math.random() * 11);
- String question = quest(a,b,i,operator);
- System.out.println(question);
- float c = a;
- float d = b;
- float ans = c/d;
- float input = sc.nextFloat();
- if (input==c/d) {
- System.out.println(correct);
- score++;
- }
- else System.out.println(wrong + ans);
- }
- default -> System.out.println("Enter a valid input!");
- }
- }
- //continuation
- if (operator <= 4 && operator >=1) {
- System.out.println("\nYour total score is " + score + " out of " + item);
- double s = score;
- double it = item;
- double grade = s / it;
- if (grade==1)
- System.out.println("\nPerfect Score!");
- else if (grade >= 0.75 && grade < 1)
- System.out.println("\nExcellent!");
- else if (grade >= 0.5 && grade < 0.75)
- System.out.println("\nGood Job");
- else if (grade >= 0.25 && grade < 0.5)
- System.out.println("\nTry Again");
- else if (grade >= 0 && grade < 0.25) {
- System.out.println("\nBetter Luck Next Time");
- } else {
- }
- }
- }
- public static String quest(int a, int b, byte i, byte operator) {
- char symbol = 0;
- switch (operator) {
- case 1 -> symbol = '+';
- case 2 -> symbol = '-';
- case 3 -> symbol = 'x';
- case 4 -> symbol = '÷';
- }
- String question = i + ". What is " + a + " " +symbol+ " " + b + "?";
- return question;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement