Skip to main content
added 3 characters in body; edited title
Source Link
Kartik
  • 163
  • 1
  • 2
  • 7

Math Problem generator using switch statement ,Random method and arrays in Java

I m new in java and created a small project in which you have to solve random math problems there is three levels in it Easy, Medium , Hard, if you win or loss in game the message for three level will be different

Math Problem generator using switch statement in Java

I m new in java and created a small project in which you have solve random math problems there is three levels in it Easy, Medium , Hard, if you win or loss in game the message for three level will be different

Math Problem generator using switch statement ,Random method and arrays in Java

I m new in java and created a small project in which you have to solve random math problems there is three levels in it Easy, Medium , Hard, if you win or loss in game the message for three level will be different

Source Link
Kartik
  • 163
  • 1
  • 2
  • 7

Math Problem generator using switch statement in Java

I m new in java and created a small project in which you have solve random math problems there is three levels in it Easy, Medium , Hard, if you win or loss in game the message for three level will be different

Flaws in my program:I didn't use float as data type so there is no question on division and % is not available

package Main;

import java.util.Random;
import java.util.Scanner;

public class Main {
public static void main(String[] args) {

    System.out.println("***************** Welcome to Game of Math ***********************");
    Scanner inputScanner = new Scanner(System.in);

    //genrating random numbers
    Random random = new Random();

    //Assing variables
    int easyNum1 = random.nextInt(11);
    int mediumNum1 =  random.nextInt(101);
    int hardNum1 =  random.nextInt(10000);

    int easyNum2 =  random.nextInt(11);
    int mediumNum2 = random.nextInt(101);
    int hardNum2 = random.nextInt(10000);

    System.out.println("Enter || E for easy || M for medium and || H for hard");
    //Getting user input for game tougness level
    char levelInput= inputScanner.next().charAt(0);



    //Genrating Random win and lose messages
    //For easy level
        //Win
    String[] winMessageEasy = {
        "Correct",
        "Yes you are right",
        "Good",
        "Your basics is good",
        "Keep going"
    };
        //Lose
    String[] lossMessageEasy = {
        "Bruhh thats wrong",
        "Nahh",
        "Wrong",
        "Boy u need to join primary school",

    };
    //for medium level
        //Win
    String[] winMessageMedium= {
            "Cool",
            "Bravo",
            "You are cool",
            "Good",
            "Oh yea",
            "oh Boy",

    };
        //Lose
    String[] loseMessageMedium={
            "Thats not look right",
            "No sorry",
            "Nope",
            "Sorry! thats inncorect",

    };
    //for Hard level
        //Win
    String[] winMessageHard = {
            "Dammm you are correct",
            "Oh yea thats right",
            "Good fella",
            "Your math is good",
            "you are a phenomena",
            "Cool bro",
            "Fab"
    };
        //Lose
    String[] loseMessageHard = {
            "Duhh",
            "mehhh",
            "nope",
            "no",
            "try again",
            "sorry! thats wrong"
    };

    //Random win message genrator for easy level
    int easyWinRandomInt = random.nextInt(winMessageEasy.length);
    String easyWinMessageOutput = winMessageEasy[easyWinRandomInt];
    //Random loss message genrator for easy level
    int easyLossRandomInt = random.nextInt(lossMessageEasy.length);
    String easyLossMessageOutput = lossMessageEasy[easyLossRandomInt];

    //Random win message genrator for medium level
    int mediumWinRandomInt = random.nextInt(winMessageMedium.length);
    String mediumWinMessageOutput = winMessageMedium[mediumWinRandomInt];
    //Random loss message genrator for medium level
    int mediumLossRandomInt = random.nextInt(loseMessageMedium.length);
    String mediumLossMessageOutput = winMessageMedium[mediumLossRandomInt];

    //Random win message genrator for hard level
    int hardWinRandomInt = random.nextInt(winMessageHard.length);
    String hardWinMessageOutput = winMessageHard[hardWinRandomInt];
    //Random loss message genrator for hard level
    int hardLossRandomInt = random.nextInt(loseMessageHard.length);
    String hardLossMessageOutput = loseMessageHard[hardLossRandomInt];






    //Switch statement for selecting level on user input
    if(levelInput == 'e' || levelInput == 'm' || levelInput == 'h'){
        switch (levelInput){
            case 'e':
                easy(easyNum1,easyNum2,easyWinMessageOutput,easyLossMessageOutput);
                break;
            case 'm':
                medium(mediumNum1,mediumNum2,mediumWinMessageOutput,mediumLossMessageOutput);
                break;
            case 'h':
                hard(hardNum1,hardNum2,hardWinMessageOutput,hardLossMessageOutput);
                break;
        }

    }else {
        System.out.println("Error! Please enter valid input E or M or H to select level");
    }


}

//Level methods
static int easy(int num1, int num2,String winMsg,String lossMsg){
    System.out.println("Easy");
    MathProblem(num1,num2,winMsg,lossMsg);

    return 0;
}
static int medium(int num1,int num2,String winMsg,String lossMsg){
    System.out.println("Medium");
    MathProblem(num1,num2,winMsg,lossMsg);
    return 0;
}
static int hard(int num1,int num2,String winMsg,String lossMsg){
    System.out.println("Hard");
    MathProblem(num1,num2,winMsg,lossMsg);
    return 0;
}



//Math methods
static int MathProblem (int num1, int num2, String winMessage, String lossMessage){

    int result = 0;
    String randomMathOp = "+-*";
    final int mathOpIndex = randomMathOp.length();
    Random r = new Random();
    Scanner ansScanner = new Scanner(System.in);
    char mathOp = randomMathOp.charAt(r.nextInt(mathOpIndex));

    switch (mathOp){

        case '+':
            result = num1+num2;
            break;
        case '-':
            result = num1-num2;
            break;
        case '*':
            result=num1*num2;
            break;

    }

    System.out.println("What is "+num1+mathOp+num2);
    int answerInput = ansScanner.nextInt();

    if(answerInput == result){
        System.out.println(winMessage);
    }
    else {
        System.out.println(lossMessage);
    }




    return 0;
}

}