-5

For some reason the switch statement doesn't work for all cases. It only works for the 2nd and 4th cases. It doesn't matter which key it is, I've swapped them around and only the 2nd and 4th actually move the coordinates.

Am I not doing this switch statement correctly?

 @Override
public void keyPressed(KeyEvent e) {
   int code = e.getKeyCode();
    switch(code) {
        case KeyEvent.VK_DOWN:
            y+=15;
        case KeyEvent.VK_UP:
            y-=15;
        case KeyEvent.VK_RIGHT:
            x+=15;
        case KeyEvent.VK_LEFT:
            x-=15;
    }
    repaint();
}
Mark
  • 51
  • 1
  • 7
  • 8
  • 2
    first you should learn java, this is not a mature question :( – Muhammad Sep 29 '14 at 13:34
  • I see people try to gain some repo from accepted answer rather than telling the questioner that this is not the place to ask such question. – Muhammed Refaat Sep 29 '14 at 13:35
  • 2
    @MuhammedRefaat: Basic questions are on-topic for SO, and `switch` is an odd construct. – T.J. Crowder Sep 29 '14 at 13:36
  • Well spotted, Nathan! – T.J. Crowder Sep 29 '14 at 13:36
  • 4
    @Muhammad You've never made a silly mistake before? I made a careless error, I think you're making premature accusations – Mark Sep 29 '14 at 13:36
  • @T.J.Crowder well, I think what Nathan did is what the others supposed to do rather that trying to gain a quick rep. just saying. at the last may be I'm not right at my comment but at least they aren't right about quick answers – Muhammed Refaat Sep 29 '14 at 13:38
  • 1
    It's the voters that contribute to reputation gain not directly the answer itself. At least 7 people disagree with you and think the answer is valuable. And yes, the oddness of `switch` does make it on-topic. If you disagree, vote to close. There is **certainly** no reason to downvote the answer. – Bathsheba Sep 29 '14 at 13:39
  • 2
    reopening because the duplicate doesn't do a good job of explaining why. see http://stackoverflow.com/questions/48017/what-is-a-jump-table for more of a rationale on what's going on here. – Nathan Hughes Sep 29 '14 at 13:44
  • @Bathsheba well, as I said may be I mis-explain why this is wrong, but what Nathan does is such what everyone has to do, you now has a multiple same question due to this answers, and this is against this community rules. – Muhammed Refaat Sep 29 '14 at 13:45
  • 1
    @Muhammed: i reopened because i couldn't find a dupe that explained why satisfactorily for me, and i think there's room here for a better answer. – Nathan Hughes Sep 29 '14 at 13:50
  • @NathanHughes But jump trees is a special case, and it's not what Dad is looking for here – Muhammed Refaat Sep 29 '14 at 13:55
  • @Dad well, I'm not judging you,and when taking about silly mistakes I make much more silly mistakes rather this one,I'm Just taking about how the others behaved towards that, you see "getlost" comment? just a word that makes you notice this silly mistake, he tried to protect you from a 5 downvotes, a closing votes, a duplicate flag,...etc. what he did is quickly notify you about the silly mistake, then you win, but he didn't win anything, in the other hand, what happened here is that Alboz took a 70 rep. while you got -10 plus being in the danger of being banned from asking anymore. – Muhammed Refaat Sep 29 '14 at 14:16

2 Answers2

6

You must add a "break;" statement in each case!

Edit your code to this:

switch(code) {
        case KeyEvent.VK_DOWN:
            y+=15;
            break;
        case KeyEvent.VK_UP:
            y-=15;
            break;
        case KeyEvent.VK_RIGHT:
            x+=15;
            break;
        case KeyEvent.VK_LEFT:
            x-=15;
            break;
    }
Alboz
  • 1,833
  • 20
  • 29
0

use this:

  public void keyPressed(KeyEvent e) 
    {
       int code = e.getKeyCode();
        switch(code) 
       {
            case KeyEvent.VK_DOWN:
                y+=15;
                break;
            case KeyEvent.VK_UP:
                y-=15;
                break;
            case KeyEvent.VK_RIGHT:
                x+=15;
                break;
            case KeyEvent.VK_LEFT:
                x-=15;
                break;
        }
        repaint();
    }

EDIT : for more information http://docs.oracle.com/javase/tutorial/java/nutsandbolts/switch.html

Imran Ali Khan
  • 8,469
  • 16
  • 52
  • 77