#Help with switch statements

1 messages · Page 1 of 1 (latest)

woeful plume
#

Hey all,

Just looking for some help with the switch statement. I'm pretty new to Java, having only picked it up this September for a course. Looking for this line of code to output either a "Correct!" or "Incorrect!" answer depending on what the user inputs

` // Q2: relational operators, switch statement
int num4 = 7, num5 = 5, num6 = 4;
System.out.println("Question 2: Is the expression 7 > 5 && 4 <= 4 false?"); //result = true, 1
System.out.println("A. True ");
System.out.println("B. False ");
System.out.println("C. None of the above ");
System.out.println("Your answer: ");
char answer2 = scanner.next().charAt(0);

        //evaluate answer using switch statement
         boolean value1 = 7 > 5 && 4 <= 4;
        switch (answer2){
            case 'A':
                System.out.println("Correct! ");
                score +=1;
                break;
            case 'B':
                System.out.println("Incorrect! The answer is A");
                break;
            case 'C':
                System.out.println("Incorrect! The answer is A ");
                break;
        }`
ornate hatchBOT
#

<@&987246399047479336> please have a look, thanks.

steady ridge
#
int num4 = 7, num5 = 5, num6 = 4;
            System.out.println("Question 2: Is the expression 7 > 5 && 4 <= 4 false?"); //result = true, 1
            System.out.println("A. True ");
            System.out.println("B. False ");
            System.out.println("C. None of the above ");
            System.out.println("Your answer: ");
            char answer2 = scanner.next().charAt(0);
            
            //evaluate answer using switch statement
             boolean value1 = 7 > 5 && 4 <= 4;
            switch (answer2){
                case 'A':
                    System.out.println("Correct! ");
                    score +=1;
                    break;
                case 'B':
                    System.out.println("Incorrect! The answer is A");
                    break;
                case 'C':
                    System.out.println("Incorrect! The answer is A ");
                    break;
            }
ornate hatchBOT
# steady ridge ```java int num4 = 7, num5 = 5, num6 = 4; System.out.println("Questi...

Detected code, here are some useful tools:

[WARNING] The code couldn't end properly...

Problematic source code:


            char answer2 = scanner.next().charAt(0);```
Cause:
The code doesn't compile:
cannot find symbol
  symbol:   variable scanner
  location: class 

Remaining code:
```java

            
            //evaluate answer using switch statement
             boolean value1 = 7 > 5 && 4 <= 4;
            switch (answer2){
                case 'A':
                    System.out.println("Correct! ");
                    score +=1;
                    break;
                case 'B':
                    System.out.println("Incorrect! The answer is A");
                    break;
                case 'C':
                    System.out.println("Incorrect! The answer is A ");
                    break;
            }```
## System out

Question 2: Is the expression 7 > 5 && 4 <= 4 false?
A. True
B. False
C. None of the above
Your answer:

steady ridge
woeful plume
#

Right now if the user inputs an answer, it just repeats back to them, no "correct" or "incorrect" output, i'm wondering what i'm missing

steady ridge
#

u entered b

#

but ur case is 'B'

#

chars are case sensitive

#

so u didnt hit any of ur cases

#

and since u didnt have a default case, nothing happened

woeful plume
#

Alright, thank you!

celest locust
#

You might also want an array to keep track of your answers there.

#

Might need multiple arrays for different answer types, but that's ok. Just a few vars and a bit of indexing maths. You can do it!

#

Really just depends on the limitations you're given.