Advertisement
AlphaPenguino

Arithmetic Quiz with Score Counter 1.3.1

Oct 28th, 2022
106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.63 KB | Source Code | 0 0
  1. package practice.programpractice;
  2. import java.util.*;
  3. public class switchmethodQuiz {
  4.    
  5.     //Author Zunder Jacob A. Pacis
  6.     // 1.3.1
  7.    
  8.     public static void main(String[] args) {
  9.        
  10.         Scanner sc = new Scanner(System.in);
  11.         System.out.println("How many items do you want to answer?");
  12.         byte item = sc.nextByte();
  13.         byte i;
  14.         System.out.println("What math operation do you want to solve?\n1 - addition\t\t2 - subtraction\n3 - multiplication\t4 - division");
  15.         byte operator = sc.nextByte();
  16.         int score = 0;
  17.        
  18.         String correct = "Your answer is correct!";
  19.         String wrong = "Your answer is wrong!, the correct answer is ";
  20.        
  21.         switch (operator) {
  22.             case 1 -> System.out.println("You have chosen Addition!\n");
  23.             case 2 -> System.out.println("You have chosen Subtraction!\n");
  24.             case 3 -> System.out.println("You have chosen Multiplication!\n");
  25.             case 4 -> System.out.println("You have chosen Division!\n");
  26.             default -> {
  27.             }
  28.         }
  29.        
  30.         for (i = 1; i <= item; i++) {
  31.         switch (operator) {
  32.             case 1 -> {
  33.                 int a = (int) (Math.random() * 11);
  34.                 int b = (int) (Math.random() * 11);
  35.                 String question = quest(a,b,i,operator);
  36.                 System.out.println(question);
  37.                 int ans = a+b;
  38.                 int input = sc.nextInt();
  39.                 if (input==a+b) {
  40.                     System.out.println(correct);
  41.                     score++;
  42.                  }
  43.                 else System.out.println(wrong + ans);
  44.                  }
  45.             case 2 -> {
  46.                 int a = (int) (Math.random() * 11);
  47.                 int b = (int) (Math.random() * 11);
  48.                 String question = quest(a,b,i,operator);
  49.                 System.out.println(question);
  50.                 int ans = a-b;
  51.                 int input = sc.nextInt();
  52.                 if (input==a-b) {
  53.                     System.out.println(correct);
  54.                     score++;
  55.                  }
  56.                 else System.out.println(wrong + ans);
  57.                  
  58.                  }
  59.             case 3 -> {
  60.                 int a = (int) (Math.random() * 11);
  61.                 int b = (int) (Math.random() * 11);
  62.                 String question = quest(a,b,i,operator);
  63.                 System.out.println(question);
  64.                 int ans = a*b;
  65.                 int input = sc.nextInt();
  66.                 if (input==a*b) {
  67.                     System.out.println(correct);
  68.                     score++;
  69.                  }
  70.                 else System.out.println(wrong + ans);
  71.                  }
  72.             case 4 -> {
  73.                 int a = (int) (Math.random() * 11);
  74.                 int b = (int) (Math.random() * 11);
  75.                 String question = quest(a,b,i,operator);
  76.                 System.out.println(question);
  77.                 float c = a;
  78.                 float d = b;
  79.                 float ans = c/d;
  80.                 float input = sc.nextFloat();
  81.                 if (input==c/d) {
  82.                     System.out.println(correct);
  83.                     score++;
  84.                  }
  85.                 else System.out.println(wrong + ans);
  86.                  }
  87.             default -> System.out.println("Enter a valid input!");
  88.          }
  89.          }
  90.           //continuation
  91.                 if (operator <= 4 && operator >=1) {
  92.                 System.out.println("\nYour total score is " + score + " out of " + item);
  93.                 double s = score;
  94.                 double it = item;
  95.                 double grade = s / it;
  96.                 if (grade==1)
  97.                     System.out.println("\nPerfect Score!");
  98.                 else if (grade >= 0.75 && grade < 1)
  99.                     System.out.println("\nExcellent!");
  100.                 else if (grade >= 0.5 && grade < 0.75)
  101.                     System.out.println("\nGood Job");
  102.                 else if (grade >= 0.25 && grade < 0.5)
  103.                     System.out.println("\nTry Again");
  104.                 else if (grade >= 0 && grade < 0.25) {
  105.                     System.out.println("\nBetter Luck Next Time");
  106.                 } else {
  107.                 }
  108.          }
  109.     }
  110.     public static String quest(int a, int b, byte i, byte operator) {
  111.         char symbol = 0;
  112.         switch (operator) {
  113.             case 1 -> symbol = '+';
  114.             case 2 -> symbol = '-';
  115.             case 3 -> symbol = 'x';
  116.             case 4 -> symbol = '÷';
  117.         }
  118.         String question = i + ". What is " + a + " " +symbol+ " " + b + "?";
  119.         return question;
  120.     }
  121. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement