Search code examples
javacalculatorjoptionpane

Creating a Random Number Calculator


Please help. I'm trying to create a Math learning system where there is a menu of 4 different mathematical operations. When an operation was selected, it will generate a set of number performing that operation. Please help me to fix the code

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

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


    Random firstnum = new Random();
    int number1 = firstnum.nextInt(9)+1;

    Random secondnum = new Random();
    int number2 = secondnum.nextInt(9);


    Scanner input = new Scanner(System.in);

    String dec;

    int firstnum,secondnum; int total = 0 ;

    System.out.println("choose letter ");
    System.out.println("A.Addition");
    System.out.println("B.SUBTRACTION");
    System.out.println("C.MULTIPLICATION");
    System.out.println("D.DIVISION");

    System.out.println("letter i choose"); 
        dec = input.next();

    if(dec.equalsIgnoreCase("a")){

        System.out.println(number1);
        firstnum = input.nextInt();

        System.out.println(number2);
        secondnum = input.nextInt();

    total = number1 + number2;

        System.out.println("Total = "+ total);
        System.out.println("Congratulation"); }


    if(dec.equalsIgnoreCase("b")){
        System.out.println(number1);
        firstnum = input.nextInt();

        System.out.println(number2);
        secondnum = input.nextInt();

    total = number1 - number2;

    System.out.println("Total = "+ total);
    System.out.println("Congratulation"); }

    if(dec.equalsIgnoreCase("c")){
        System.out.println(number1);
        firstnum = input.nextInt();

        System.out.println(number2);
        secondnum = input.nextInt();

    total = number1 * secondnum;

    System.out.println("Total = "+ total);
    System.out.println("Congratulation"); }

    if(dec.equalsIgnoreCase("d")){
        System.out.println(number1);
        firstnum = input.nextInt();

        System.out.println(number2);
        secondnum = input.nextInt();

    total = firstnum / secondnum;

        System.out.println("Total = "+ total);
        System.out.println("Congratulation"); }

        else System.exit(0);
}

}


Solution

  • You're declaring firstnum and secondnum two times

    Random firstnum = new Random();
    
    Random secondnum = new Random();
    

    And

    int firstnum,secondnum; int total = 0 ;
    

    Remove / rename one of these.