Search
 
SCRIPT & CODE EXAMPLE
 

JAVA

maths trivia in java

import java.util.Scanner;

public class Project03 {

    public static void main(String[] args) {
        Scanner keyboard = new Scanner(System.in);
        System.out.print("Enter your name:");
        String name = keyboard.nextLine();
        System.out.print("Welcome " + name + "! Please answer the following questions:");

        int randomNumber1 =  (int)(20 * Math.random()) + 1;
        int randomNumber2 =  (int)(20 * Math.random()) + 1;
        int randomNumberAdd = randomNumber1 + randomNumber2;
        int randomNumberMul = randomNumber1 * randomNumber2;
        int randomNumberDiv = randomNumber1 / randomNumber2;
        int randomNumberRem = randomNumber1 % randomNumber2;
        double correct = 0;
        double percentageCorrect = correct * 25;


        System.out.print(randomNumber1 + " + " + randomNumber2 + " = ");
        int GuessRandomNumberAdd = keyboard.nextInt();
        if (GuessRandomNumberAdd == randomNumber1 + randomNumber2) {
            System.out.println("Correct!");
            correct++;
        }
        else {
            System.out.println("Wrong!");
            System.out.println("The correct answer is " + randomNumberAdd);

            System.out.print(randomNumber1 + " * " + randomNumber2 + " = ");
            int GuessRandomNumberMul = keyboard.nextInt();
            if (GuessRandomNumberMul == randomNumber1 * randomNumber2) {
                System.out.println("Correct!");
                correct++;
            }
            else{
                System.out.println("Wrong!");
                System.out.println("The correct answer is " + randomNumberMul);
            }

            System.out.print(randomNumber1 + " / " + randomNumber2 + " = ");
            int GuessRandomNumberDiv = keyboard.nextInt();
            if (GuessRandomNumberDiv == randomNumber1 / randomNumber2) {
                System.out.println("Correct!");
                correct++;
            }
            else{
                System.out.println("Wrong!");
                System.out.println("The correct answer is " + randomNumberMul);


                System.out.print(randomNumber1 + " % " + randomNumber2 + " = ");
                int GuessRandomNumberRem = keyboard.nextInt();
                if (GuessRandomNumberRem == randomNumber1 % randomNumber2) {
                    System.out.println("Correct!");
                    correct++;
                }
                else{
                    System.out.println("Wrong!");
                    System.out.println("The correct answer is " + randomNumberRem);

                    System.out.println("You got " + correct + " correct answers.");

                    System.out.println("That's " + percentageCorrect + "%!");
                }
            }
        }
    }
}
Comment

PREVIOUS NEXT
Code Example
Java :: get value from Spring application.properties 
Java :: java printwriter create file 
Java :: java how to split a string to array 
Java :: Java List Access Elements using get() method 
Java :: lambda expression java 
Java :: Java Method Create Basic 
Java :: java data structure 
Java :: java collectors mapping 
Java :: java float data type 
Java :: convert string into unicode java 
Java :: can abstract class have non abstract methods in java 
Java :: what is encapsulation in java 
Java :: How to perform in-order traversal of a binary tree? 
Java :: loop and save letters in a string java 
Java :: try catch block 
Java :: java replaceall single character 
Java :: java array 
Java :: how to create microservices architecture with spring boot 
Java :: java eth 
Java :: How to send saved CSV file via email 
Java :: Multi basic auth with spring security 
Java :: Java program to swap two numbers using bitwise xor operator 
Java :: java method overloading 
Java :: jbutton default color 
Java :: clor text consol java 
Java :: android java navigation bar show icon with title 
Java :: Java TestNG Data Provider 
Java :: action listener for button to close window java 
Java :: java function that returns the index of the largest value in an array 
Java :: find node from pos linkedlist java 
ADD CONTENT
Topic
Content
Source link
Name
7+6 =