Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * To change this license header, choose License Headers in Project Properties.
- * To change this template file, choose Tools | Templates
- * and open the template in the editor.
- */
- package hoa2mathapp_dela.cruz;
- import java.util.Scanner;
- import java.util.Random;
- /**
- *
- * @author Grade 10
- */
- public class HOA2MathApp_DelaCruz {
- /**
- * @param args the command line arguments
- */
- public static void main(String[] args) {
- int v = 0;
- Scanner zz = new Scanner(System.in);
- Random zed= new Random();
- String retry = "YES";
- while (v==0){ //in order to loop infinitely until the user decides to end the program
- if(retry.equals("YES") || retry.equals("Y")){ //checker if it should loop
- int z = 0;
- int ta;
- int in;
- System.out.println("~ WELCOME TO THE MATH CHALLENGE ~");
- System.out.println("TO begin, please select an operator: ");
- System.out.println("||+||-||*||/||");
- char operator = zz.next().charAt(0); //to get the user's chosen operator
- switch (operator){ //switch case for varying operators
- case '+':
- System.out.println("You have selected Addition!");
- while (z < 5){ // counter & limit
- int r1 = zed.nextInt(15); //to get a random number from 1-50 for r1
- int r2 = zed.nextInt(15); //to get a random number from 1-50 for r2
- System.out.println(r1 + " + " + r2);
- ta = r1 + r2; //computes the correct answer
- in = zz.nextInt();
- if (in == ta){ // verifies if user's answer is correct or not
- System.out.println("You are Correct!");
- z++; //counter & limit
- }
- else {
- System.out.println("That is the Wrong Answer!");
- System.out.println(" The Answer is " + ta);
- }
- }
- break;
- // same rules of codes are applied below
- case '-':
- System.out.println("You have chosen Subtraction!");
- while (z < 5){
- int r1 = zed.nextInt(15);
- int r2 = zed.nextInt(15);
- System.out.println(r1 + " - " + r2);
- ta = r1 - r2;
- in = zz.nextInt();
- if (in == ta){
- System.out.println("You are Correct!");
- z++;
- }
- else {
- System.out.println("That is the Wrong Answer!");
- System.out.println(" The Answer is " + ta);
- }
- }
- break;
- case '*':
- System.out.println("You have selected Multiplication!");
- while (z < 5){
- int r1 = zed.nextInt(15);
- int r2 = zed.nextInt(15);
- System.out.println(r1 + " * " + r2);
- ta = r1 * r2;
- in = zz.nextInt();
- if (in == ta){
- System.out.println("Correct!");
- z++;
- }
- else {
- System.out.println("That is the Wrong Answer!");
- System.out.println(" The Answer is " + ta);
- }
- }
- break;
- case '/':
- System.out.println("You have chosen Division!");
- while (z < 5){
- int r1 = zed.nextInt(15);
- int r2 = zed.nextInt(15);
- System.out.println(r1 + " / " + r2);
- ta = r1 / r2;
- in = zz.nextInt();
- if (in == ta){
- System.out.println("Correct!");
- z++;
- }
- else {
- System.out.println("Wrong Answer!");
- System.out.println(" The Answer is " + ta);
- }
- }
- break;
- default :
- System.out.println("\nPlease input a valid operator!");
- }
- System.out.println("Would you like to try again?");
- System.out.println("YES or NO");
- retry = zz.next().toUpperCase(); //so that the user doesn't have to worry on monitoring wether it's uppercase or not
- }
- else {
- zz.close(); //closes scanner
- break; //breaks loop
- }
- }
- System.out.println("Thank you come again!");
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement