vendredi 24 juillet 2015

In my if statements even though (levelsub==1) , (and levelsub is a int not a String!) is true, it doesn't run my code. Java, eclipse

I am trying to make a math game, and I have a problem with the subtraction part. I let the user choose whether to play with positive or negative numbers, and how high the numbers are. My game doesn't run the appropriate if statements based on the user's input.

I have looked at many questions on this site, and have accordingly changed my if-statements to if-else-statements. I have also noticed that most people who have problems with if statements are comparing Strings using "==". I am comparing ints, so I don't think that this is my problem. I have also read through the code many, many times and compared it to projects I have done in the past. I am probably just missing something simple because I have done things just like this before that have worked! I really appreciate any help!

Here is my code. In my actual game, instead of telling you what you chose("System.out.println("You chose subtraction, positive, less than 100.");) as I put in the code I am posting for you, the if-statements tell the program to run a certain method based on what the user's choices were. Also, the words like "hello" and "I am here" aren't relevant to the game, I put them in to show where my game is going wrong. If you run this code, you should press 2 for subtraction, and then no matter what you push next, the same thing will happen--that's the problem.

I am a beginner. Thank you so much to anyone who can help!

import java.util.Scanner;

public class MainMathGameStackOverflow {
    public static int levelsub;
    public static int choice;
    public static int posneg;
    public static Scanner scanning = new Scanner(System.in);

    public static void main(String[] args) {
        System.out.println("MATH");
            operation();
            keepgoing();
    }

    //choose which operation you want to do
    public static void operation() {
        System.out.println("If you want to do addition, press 1.");
        System.out.println("If you want to do subtraction, press 2.");
        System.out.println("If you want to do multiplication, press 3.");
        System.out.println("If you want to do division, press 4.");
        System.out.println("If you are done playing, press 5.");
        choice = scanning.nextInt();

        operationrunner();
    }

    //run if statements based on which operation was chosen
    public static void operationrunner() {

        //subtraction
        if (choice == 2) {
            //running the choice between positive and negative numbers
            subnegpositive(levelsub);
        }
        //subtraction now runs subnegpositive to choose if you want negative or positive numbers

    }

    //choosing negative/positive numbers for subtraction
    public static void subnegpositive(int levelsub) {
        System.out.println("Do you want to play with negative or only postive numbers?");
        System.out.println("If you want to play with only postive numbers, press 1.");
        System.out.println("If you want to play with only negative numbers, press 2.");
        System.out.println("If you want to play with negative and postive numbers, press 3.");
        posneg = scanning.nextInt();

        System.out.println("I'm about to run runsubtraction");
        runsubtraction(levelsub, posneg);
        System.out.println("I ran runsubtraction");
    }
    //subtraction will run runsubtraction to start the methods

    public static void runsubtraction(int levelsub, int posneg) {   
        //just positive
        if (posneg == 1) {
            sublevel();
            //this will run to choose how high the numbers are
        }   
            //run positive subtraction methods
            if (levelsub == 1) {
                System.out.println("It works");
                System.out.println("This is subtraction positive less than 50.");
            }
            else if (levelsub == 2) {
                System.out.println("This is the problem.");
                System.out.println("This is subtraction positive less than 100.");
            }
            else if (levelsub == 3) {
                System.out.println("This is subtraction positive less than 200.");
            }
            else if (levelsub == 4) {
                System.out.println("This is subtraction positive less than 500.");
            }
            else if (levelsub == 5) {
                System.out.println("This is subtraction positive less than 1000.");
            }
            System.out.println("Hello");    
        //just negative 
        if (posneg == 2) {
            System.out.println("Im about to run sublevel");
            sublevel();
            System.out.println("I ran sublevel");
            //this will run to choose how high the numbers are
            //running negative subtraction methods
            if(levelsub == 1) {
                System.out.println("This is subtraction negative less than 50.");
            }
            if (levelsub == 2) {
                System.out.println("This is subtraction negative less than 100.");
            }
            if (levelsub == 3) {
                System.out.println("This is subtraction negative less than 200.");
            }
            if (levelsub == 4) {
                System.out.println("This is subtraction negative less than 500.");
            }
            if (levelsub == 5) {
                System.out.println("This is subtraction negative less than 1000.");
            }

        }
        //negative and positive
            if (posneg == 3) {
            sublevel();
            }
            //this will run to choose how high the numbers are
            //run neg/pos subtraction methods
            else if (levelsub == 1) {
                System.out.println("hi");
                System.out.println("This is subtraction negative and positive less than 50.");
            }
            else if (levelsub == 2) {
                System.out.println("This is subtraction negative and positive less than 100.");
            }
            else if (levelsub == 3){
                System.out.println("This is subtraction negative and positive less than 200.");
            }
            else if (levelsub == 4) {
                System.out.println("This is subtraction negative and positive less than 500.");
            }
            else if (levelsub == 5) {
                System.out.println("This is subtraction negative and positive less than 1000.");
            }

    }

    //choosing how high subtraction numbers should be
    public static void sublevel() {
        System.out.println("How high do you want the numbers to be?");
        System.out.println("If you want to subtract numbers less than 50, press 1.");
        System.out.println("If you want to subtract numbers less than 100, press 2.");
        System.out.println("If you want to subtract numbers less than 200, press 3.");
        System.out.println("If you want to subtract numbers less than 500, press 4.");
        System.out.println("If you want to subtract numbers less than 1000, press 5.");
        levelsub= scanning.nextInt();


    }
    //end of game--keep going or stop?
    public static void keepgoing() {

        System.out.println("To continue playing, press 1.");
        System.out.println("To be finished, press 2");
        int keepplaying = scanning.nextInt();

        if (keepplaying == 1) {
            operation();
            keepgoing();
        }
        if (keepplaying == 2) {
            System.out.println("Thanks for playing. Come back soon!");
        }

    }


}

Aucun commentaire:

Enregistrer un commentaire