4

a new user here... and I feel my code is wrong.

I'd like to ask for assistance since I am relatively new to the program of Greenfoot. The problem at hand is as stated in the question: I am receiving an "incompatible types" error when compiling my code and I can't seem to fix it regardless of any modification I do. The specific part of code where the problem lies is as follows:

        public void answerValidation()
      {
          int ansCorrect = 0;
          int ansIncorrect = 0;

          for(int i = 0; i <= 10; i++)
          {
              answerArray[0] = array1[0] * array2[0];
              if(answer != answerArray[0])
              {
                  ansIncorrect = ansIncorrect + 1;
                  JOptionPane.showMessageDialog(null, array1[0] + "*" + array2[0] + "=" + answerArray[0]);
              }
              else
              {
                  ansCorrect = ansCorrect + 1;
              }
          }

      switch(ansCorrect)
      {
          case 10:  JOptionPane.showMessageDialog(null, "Wow! You got all the questions right!");
                    break;

          case ansCorrect>=8 && ansCorrect < 9:    JOptionPane.showMessageDialog(null, "You scored" + ansCorrect + "out of 10. 80%-90% scored.");
                    break;

          case ansCorrect >=6 && ansCorrect < 7:   JOptionPane.showMessageDialog(null, "You scored" + ansCorrect + "out of 10. Keep practicing in Lv2 to improve.");
                    break;

          case ansCorrect == 0 && ansCorrect < 6:   JOptionPane.showMessageDialog(null, "You scored" + ansCorrect + "out of 10. Keep practicing in Lv1 to improve.");
                    break;
      }

}

The compiling error states that it is located where it says "&& ansCorrect < 9", although I don't know how to fix it.

Any corrections/requests to see my code are welcome, and much thanks to those who help!

Blitz
  • 53
  • 5

3 Answers3

2
case ansCorrect>=8 && ansCorrect < 9: 

param in the switch case must be an int wnere case ansCorrect>=8 && ansCorrect < 9: resolved to an boolean .

And if you see the logic at ansCorrect>=8 && ansCorrect < 9

That exactly case 8 ?? and same with case ansCorrect >=6 && ansCorrect < 7: that should write as case 6.

and regarding case ansCorrect == 0 && ansCorrect < 6 , you can write multiple cases with same functionality

case 0;
case 1;
case 2;
case 3;
case 4;
case 5;
JOptionPane.showMessageDialog(null, "You scored" + ansCorrect + "out of 10. Keep practicing in Lv1 to improve.");
break;
Suresh Atta
  • 120,458
  • 37
  • 198
  • 307
  • Interesting! I never knew that you could execute cases like that! – Blitz Sep 11 '15 at 08:12
  • 1
    @Blitz Yes. There are few things you need to know about switch :https://docs.oracle.com/javase/tutorial/java/nutsandbolts/switch.html – Suresh Atta Sep 11 '15 at 08:14
2

You can't use case like that. It's not an alternative way to write an if clause, like it seems you're thinking.

Kayaman
  • 72,141
  • 5
  • 83
  • 121
  • Oh.. I always assumed that the "case" statement was made for reviewing multiple outcomes. – Blitz Sep 11 '15 at 08:07
  • 1
    @Blitz It is, but multiple outcomes of the `switch(foo)` part, not the `case` which requires a single option (`int` or these days `String`, which is turned to ints in the bytecode). – Kayaman Sep 11 '15 at 08:11
1

You cannot use boolean expressions in case:. Instead you can do the following:

 if(ansCorrect>=8 && ansCorrect < 9){
    JOptionPane.showMessageDialog(null, "You scored" + ansCorrect + "out of 10. 80%-90% scored.");
    }

Also as a quick suggestion - this: ansCorrect = ansCorrect + 1 may be simplified to ansCorrect++

EDIT: Also have in mind that while multiple case switches may solve the issue, but you will have problems if you at some point switch to double e.g. 5.15 instead of 5

Phantomazi
  • 408
  • 6
  • 22
  • Interesting, I wasn't aware that you could increment/decrement values without entering a for loop. Must study Java a lot more. – Blitz Sep 11 '15 at 08:11
  • 1
    If you want to find out more about Java incrementing/decrementing i suggest you look at some nicely explained tutorial - like this one: http://www.freejavaguide.com/increment_decrement_operators.htm :) Also, as I can see you are new to SO, as a friendly reminder - when 15 mins are passes since you ask a question and there are answers which fix your issue - make sure you choose one of them, so the question appears as solved :) – Phantomazi Sep 11 '15 at 08:13