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 3 of 3

Thread: Using variables in switch statement case clauses

  1. #1
    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 in switch statement case clauses

    I am stuck with two switch statements that I feel I am close to achieving. My first switch statement is to generate random mathematical operators for the math questions in the game (generated randomly) My second switch statement is to determines which action the system will take upon the correct or incorrect answer. For the first switch statement I have a variable called num. Num is a random integer. I have an operator mapped to each outcome of num. I may know the solution to fix this. In my second switch statement I am trying to use the variable answer as a variable in the first case but do not know how. Advice?



    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);
            int num = randomnum.nextInt(4);
            char operator;
     
     
            //Switch statement for random operator
            switch (num) {
                case 1: operator = '+';
                    break;
                case 2: operator = '-';
                    break;
                case 3: operator = '*';
                    break;
                case 4: operator = '/';
                    break;
            }
     
     
            //Declare and construct variables of math problem
            fnum = mathnumber;
            snum = newnumber;
            answer = fnum + snum;
     
            //Math problem 10x loop
     
     
            //Output of math problem
            System.out.print(fnum);
            System.out.print(num);
            System.out.println(snum);
     
            //User input
            Scanner serena = new Scanner(System.in);
            int userAnswer = serena.nextInt();
     
            //Switch statement for corect/incorrect answer
            switch (answer) {
                case answer: 
                    System.out.println("correct!");
                    break;
                default: 
                    System.out.print("incorrect! The correct answer is:");
                    System.out.println(answer);
                    break;
            }
     
        }
    }


  2. #2
    Senior Member
    Join Date
    Jul 2013
    Location
    Europe
    Posts
    858
    Thanks
    0
    Thanked 158 Times in 141 Posts

    Default Re: Using variables in switch statement case clauses

    The cases in a switch statement must be constants and not variables. If you want to use variables you have to use an if-elseif block.

  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: Using variables in switch statement case clauses

    That is fine. I had an if else block before and it worked. I am just practicing and seeing what I can and cannot do. My primary concern is generating random math operators to appear in the question.

Similar Threads

  1. using case switch statement. display salary and bonus.. help me
    By yarnue14 in forum What's Wrong With My Code?
    Replies: 5
    Last Post: October 11th, 2013, 09:54 PM
  2. switch case
    By viyyapu harsha in forum What's Wrong With My Code?
    Replies: 2
    Last Post: June 24th, 2013, 01:03 AM
  3. Replies: 9
    Last Post: February 24th, 2013, 06:51 PM
  4. Need help with switch case
    By niko25 in forum What's Wrong With My Code?
    Replies: 3
    Last Post: October 18th, 2012, 01:10 AM
  5. Case Switch Help?
    By xionyus in forum What's Wrong With My Code?
    Replies: 17
    Last Post: October 19th, 2011, 09:59 PM