Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 13 of 13

Thread: Switch statements to initialize variable due to multiple outcomes?

  1. #1
    Junior Member
    Join Date
    May 2014
    Location
    Java, TX
    Posts
    18
    My Mood
    Where
    Thanks
    6
    Thanked 0 Times in 0 Posts

    Unhappy Initializing answer variable.

    After this problem is fixed, my math game will run. The answer variable is supposed to be equal to the cases in the second switch statement by random but I get errors whenever I try to initialize it this way. I have been writing this program for almost 5 days now and it is finally wrapping up. Does anyone know why I can't map the answer variable to the second switch statement the way I want to?

    package pkgnew;
     
    import java.util.Scanner;
    import java.util.Random;
     
    public class New {
     
        public static void main(String args[]) {
     
            //Declare and construct variables 
            Random randomnum = new Random();
            int fnum, snum, answer;
            int mathnumber = randomnum.nextInt(20);
            int newnumber = randomnum.nextInt(20);
     
            //Declare and construct variables of math problem
            fnum = mathnumber;
            snum = newnumber;
     
            //Declare random operator variable
            String[] operators = {"+" , "-", "*", "/" };
            int randomIndex = randomnum.nextInt(3);
            String symbol = operators[randomIndex];
     
            //Switch statement for random operator and question display
            switch (symbol) {
                case "+": 
                    System.out.println(fnum + "+" + snum);
                    break;
                case "-":
                    System.out.println(fnum + "-" + snum);
                    break;
                case "*":
                    System.out.println(fnum + "*" + snum);
                    break;
                case "/":
                    System.out.println(fnum + "/" + snum);
                    break;
            }
     
            //Declare random answer variable
            String[] solutions = {fnum + "+"+ snum, fnum + "-" + snum, 
            fnum + "*" + snum, fnum + "/" + snum };
            int randomCase = randomnum.nextInt(3);
            String userInput = solutions[randomCase];
     
            //Switch statement to map answer to question display
            switch (userInput) {
            case "fnum + \"+\"+ snum":
                answer = fnum + snum;
                break;
            case "fnum + \"-\" + snum":
                answer = fnum - snum;
                break;
            case "fnum + \"*\" + snum":
                answer = fnum * snum;
                break;
            case "fnum + \"/\" + snum":
                answer = fnum / snum;
                break;
            }
     
            //User input
            Scanner serena = new Scanner(System.in);
            int userAnswer = serena.nextInt();
     
            //If user input = answer display "correct"
            if (userAnswer == answer) {
            System.out.println("Correct!");
     
            //If user input does not = answer display "wrong" and correct answer
            } else {
            System.out.print("Wrong! The correct answer is: ");
            System.out.print(answer);
            }
     
    }
    }


  2. #2
    Senior Member
    Join Date
    Jul 2013
    Location
    Europe
    Posts
    666
    Thanks
    0
    Thanked 121 Times in 105 Posts

    Default Re: Initializing answer variable.

    First of all, the error message would probably be of much help.
    Second, if I had to guess (which I have, because you did not provide us with the error message) I would say that there are certain cases in which the variable will never be initialized which is forbidden in java.

  3. #3
    Junior Member
    Join Date
    May 2014
    Location
    Java, TX
    Posts
    18
    My Mood
    Where
    Thanks
    6
    Thanked 0 Times in 0 Posts

    Default Re: Initializing answer variable.

    Well if it is trouble to you,do not respond. It will only cause more trouble for you.

  4. #4
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: Initializing answer variable.

    Quote Originally Posted by kunimaro15689 View Post
    Well if it is trouble to you,do not respond. It will only cause more trouble for you.
    Why the attitude? Let's say it's simply a misunderstanding due to language differences.

    From that perspective, it's common courtesy and widely advised throughout the forum to post the error message(s) you want help with. Copy and paste them into a post from exactly as they appear at your end. If the errors are logical rather than compiler or run time, then describe the errors you want help with as best you can.

    Guessing what you want help with is typically unproductive.

  5. #5
    Junior Member
    Join Date
    May 2014
    Location
    Java, TX
    Posts
    18
    My Mood
    Where
    Thanks
    6
    Thanked 0 Times in 0 Posts

    Default Switch statements to initialize variable due to multiple outcomes?

    I made a math game with the variable "answer" being the actual answer. But the actual answer is dependent upon the math question. Can I use a switch statement to initialize the variable and map it to the outcomes of the original switch statement? It is possible to map switch statements to each other. I have done it. The question is; is it possible to initialize a variable via switch statement due to multiple outcomes?

  6. #6
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: Switch statements to initialize variable due to multiple outcomes?

    Please stop posting duplicate or nearly duplicate threads. Hang with one thread and those trying to help you.

    Threads merged.

  7. #7
    Junior Member
    Join Date
    May 2014
    Location
    Java, TX
    Posts
    18
    My Mood
    Where
    Thanks
    6
    Thanked 0 Times in 0 Posts

    Default Using variables initialized in a switch statement.

    I am making a math game that asks the user elementary questions.I have no errors. The answer variable was to be initialized in my second switch statement with the value 0 being default for the variable. However, when I run the program, I always get the answer 0. The answer variable has to be initialized but i need it to be so with the solution of the equations in the second switch statement. How do I initialize a variable that has multiple possibilities?

    package pkgnew;
     
    import java.util.Scanner;
    import java.util.Random;
     
    public class New {
     
        public static void main(String args[]) {
     
            //Declare and construct variables 
            Random randomnum = new Random();
            int fnum, snum, answer;
            int mathnumber = randomnum.nextInt(20);
            int newnumber = randomnum.nextInt(20);
     
            //Declare and construct variables of math problem
            fnum = mathnumber;
            snum = newnumber;
     
            //Declare random operator variable
            String[] operators = {"+" , "-", "*", "/" };
            int randomIndex = randomnum.nextInt(3);
            String symbol = operators[randomIndex];
     
            //Switch statement for random operator and question display
            switch (symbol) {
                case "+": 
                    System.out.println(fnum + "+" + snum);
                    break;
                case "-":
                    System.out.println(fnum + "-" + snum);
                    break;
                case "*":
                    System.out.println(fnum + "*" + snum);
                    break;
                case "/":
                    System.out.println(fnum + "/" + snum);
                    break;
            }
     
            //Declare answer variable
            String[] solutions = {fnum + "+"+ snum, fnum + "-" + snum, 
            fnum + "*" + snum, fnum + "/" + snum };
     
            //Switch statement to map answer to question display
            switch (solutions[3]) {
            case "fnum + \"+\" + snum":
                answer = fnum+snum;
                break;
            case "fnum + \"-\" + snum":
                answer = fnum-snum;
                break;
            case "fnum + \"*\" + snum":
                answer = fnum*snum;
                break;
            case "fnum + \"/\" + snum":
                answer = fnum/snum;
                break;
            default: answer = 0;  
            }
     
            //User input
            Scanner serena = new Scanner(System.in);
            int userAnswer = serena.nextInt();
     
            //If user input = answer display "correct"
            if (userAnswer == answer) {
            System.out.println("Correct!");
     
            //If user input does not = answer display "wrong" and correct answer
            } else {
            System.out.print("Wrong! The correct answer is: "+answer);
            }
     
    }
    }

  8. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Using variables initialized in a switch statement.

    I always get the answer 0.
    Try debugging the code by adding some println() statements that print out the values of the variables used and the array so you can see what the computer sees when the code is executed. The print out will help you understand what the code is doing.

    Use the Array class's toString() method to print the array's contents:
    System.out.println("an ID "+ java.util.Arrays.toString(theArrayName));


    Also there should be default: cases in the switch statement to catch unexpected cases.
    If you don't understand my answer, don't ignore it, ask a question.

  9. #9
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: Switch statements to initialize variable due to multiple outcomes?

    Please stop posting duplicate threads. This is the second time you've been asked. Future infractions may require additional action.

  10. #10
    Junior Member
    Join Date
    May 2014
    Location
    Java, TX
    Posts
    18
    My Mood
    Where
    Thanks
    6
    Thanked 0 Times in 0 Posts

    Default Re: Switch statements to initialize variable due to multiple outcomes?

    The threads you merged were the only two.

  11. #11
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: Switch statements to initialize variable due to multiple outcomes?

    How do I initialize a variable that has multiple possibilities?
    If you know the value of the variable will be properly set later - in the switch statement, for example - then initialize the variable to zero or perhaps some other value (-9999999.9) that will indicate that an error has occurred if the switch statement assignment fails for some reason.

  12. #12
    Junior Member
    Join Date
    May 2014
    Location
    Java, TX
    Posts
    18
    My Mood
    Where
    Thanks
    6
    Thanked 0 Times in 0 Posts

    Default Re: Switch statements to initialize variable due to multiple outcomes?

    Thanks, Greg. I have defaulted the answer to 0 and that is always the result when I run the game. This leads me to believe that my second switch statement is a bit wrong. In the first I use random operators to display all of the possibilities of the equations I wish to display. The second switch is based off of the results of the first switch. For example: if the results of the first switch says to display fnum + snum randomly, the second switch is to find that and calculate the answer to compare the user's answer to it's own. So the results of the first switch are the cases in the second. If the results and the cases match then I should have the answer variable problem solved. I am just not sure how to find the random results of the first switch with the second

  13. #13
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Switch statements to initialize variable due to multiple outcomes?

    The case statements take constant values. If you need to have variables in the case, then you need to use another type of selection code like an if/else if chain.
    The String: "fnum" has nothing to do with the variable: fnum.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Replies: 1
    Last Post: October 25th, 2013, 10:43 PM
  2. Im very new to java And switch statements
    By RevChe in forum Loops & Control Statements
    Replies: 9
    Last Post: March 17th, 2013, 10:14 AM
  3. IF and SWITCH Statements: How and When to Use Them
    By snowguy13 in forum Java Programming Tutorials
    Replies: 2
    Last Post: January 11th, 2012, 10:46 AM
  4. Need switch statement for checker game (DUE TOMORROW!)
    By strengthonor in forum Loops & Control Statements
    Replies: 1
    Last Post: September 16th, 2011, 03:40 AM
  5. Help with switch statements
    By suxen in forum Loops & Control Statements
    Replies: 4
    Last Post: February 15th, 2011, 04:55 AM