Java won't run because there a break statement outside a loop, but I have it inside a loop -


i'm having 2 weird errors

new error when tell java draw string displays coordinateness of x , y, doesn't.

public void paint (graphics g) {     super.paint (g);     //system.out.println ("boolean: " + this.closedoors);       g.drawstring("("+x+","+y+")",x,y); } 

link program if compile it. http://hotfile.com/dl/107032853/c81d927/pigment.java.html

this complete program

/*  * change template, choose tools | templates  * , open template in editor.  */ import javax.swing.*; import java.awt.*; import java.awt.event.*; import java.applet.applet; import java.awt.graphics;  /**  *  * @author george beazer  */ public class pigment extends japplet  {     boolean closedoors;     private int x = 0;     private int y = 0;       public static void main(string [] args)     {             pigment stuff = new pigment();     }     public pigment()      {          setbackground (color.blue);     }      @override     public void init()     {          setlayout(new flowlayout());          addmouselistener(new mymouselistener());     }     @override     public void paint (graphics g)     {         super.paint (g);         //system.out.println ("boolean: " + this.closedoors);           g.drawstring("("+x+","+y+")",x,y);          if (x > 35)              {                             g.drawline (35, 50, 570, 50);                 g.drawline (35, 50, 250, 0);                 g.drawline (250, 0, 570, 50);                 g.drawrect (50, 50, 500, 350);                 g.fillrect (100, 75, 80, 80);                 g.fillrect (400, 75, 80, 80);                 g.fillrect (240, 200, 125, 200);               }          else             {                     g.drawline (35, 50, 570, 50);             g.drawline (35, 50, 250, 0);             g.drawline (250, 0, 570, 50);             g.drawline (180, 120, 100, 120);             g.drawline (400, 120, 480, 120);             g.drawline (140, 75, 140, 160);             g.drawline (450, 75, 450, 160);             g.drawrect (50, 50, 500, 350);             g.drawrect (100, 75, 80, 80);             g.drawrect (400, 75, 80, 80);             g.drawrect (240, 200, 125, 200);             g.drawoval (330,280, 20, 20);             }       }     private class mymouselistener implements mouselistener     {         public void mouseclicked (mouseevent e)         {             x = e.getx();             y = e.gety();          }          public void mouseentered (mouseevent e)         {           }         public void mouseexited(mouseevent e){}         public void mousepressed (mouseevent e){          }         public void mousereleased (mouseevent e){}      }  } 

with regards first question, reason you're getting compiler error here:

if (x > 35) {     g.drawline (35, 50, 570, 50);     g.drawline (35, 50, 250, 0);      repaint();     break;   } 

is break statement not in loop. in java, can break out of while, for, do...while, , switch statements, not if statements. there's not particularly reason - it's historical - compiler indeed enforce it.

of 3 aforementioned control structures, for, while, , do...while referred loops because execute code potentially many times. break statement in case way of saying "please abort execution of current loop; don't want run more." opposite continue, means "please go next iteration of loop."

the reason can break out of switch because java's switch statement based on c programming language's version of switch in labels "fall-through." in context, break means "i have finished executing of code want execute in particular label; please me out of statement." interestingly, can break out of switch, can't continue.

however, cannot break out of if statement. there no high-level reason this, , in fact language designers have allowed behavior mean "stop executing part of if statement." think reason opted not if statements have sort of "implicit break" @ end of each handler. example, if write

if (condition()) {     // } else {     // b } // c 

then after executing a, control flow jump c, rather falling through else handler.

if want simulate break out of of middle of if statement, this:

if (condition()) {      // code       if (someothercondition()) {           // more code      } } 

the idea here run if statement time, decide using second if statement whether or not run rest of code in loop.

hope helps!


Comments

Popular posts from this blog

Javascript line number mapping -

c# - Is it possible to remove an existing registration from Autofac container builder? -

php - Mysql PK and FK char(36) vs int(10) -