Java won't run because there a break statement outside a loop, but I have it inside a loop
I'm having two weird errors
New error is when i tell java to draw a string that displays the coordinateness of x and y, It doesn't.
public void paint (Graphics g)
{
super.paint (g);
//System.out.println ("Boolean: " + this.closeDoors);
g.drawString("("+x+","+y+")",x,y);
}
Link to my program if you to compile it. http://hotfile.com/dl/107032853/c81d927/Pigment.java.html
This is my complete program
/*
* To change this template, choose Tools | Templates
* and open the template in the 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(Stri开发者_C百科ng [] 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 to your first question, the reason that you're getting a compiler error here:
if (x > 35)
{
g.drawLine (35, 50, 570, 50);
g.drawLine (35, 50, 250, 0);
repaint();
break;
}
Is that this break
statement is not actually in a loop. In Java, you can break out of while
, for
, do...while
, and switch
statements, but not if
statements. There's not a particularly good reason for this - it's mainly historical - but the compiler does indeed enforce it.
Of the three aforementioned control structures, for
, while
, and do...while
are referred to as loops because they execute code potentially many times. The break
statement in this case is a way of saying "please abort execution of the current loop; I don't want to run it any more." Its opposite is continue
, which means "please go to the next iteration of this loop."
The reason you can break out of a switch
is because Java's switch
statement is based on the C programming language's version of switch
in which labels are "fall-through." In this context, break
means "I have finished executing all of the code I want to execute in this particular label; please get me out of this statement." Interestingly, you can break
out of a switch
, but you can't continue
.
However, you cannot break
out of an if
statement. There is no high-level reason for this, and in fact the language designers could just as easily have allowed this behavior to mean "stop executing this part of the if
statement." I think the reason they opted not to do this is that if
statements have a sort of "implicit break
" at the end of each handler. For example, if you write
if (condition()) {
// A
} else {
// B
}
// C
Then after executing A
, the control flow will immediately jump you to C
, rather than falling through to the else
handler.
If you want to simulate a break
out of of the middle of an if
statement, you could do something like this:
if (condition()) {
// code
if (someOtherCondition()) {
// more code
}
}
The idea here is that you run the if
statement for some time, then decide using a second if
statement whether or not to run the rest of the code in the loop.
Hope this helps!
if (x > 35) {...}
is not a loop it is a statement.
for (int x = 0; x <= 35; x++) {...}
is a loop.
while( x <= 35 ) {...}
is a loop.
do {...} while ( x <= 35 )
is a loop.
Take the repaint()
call out it is redundant.
You really need to go and click the CheckMark on the answers that are "Accepted" on your previous questions, people are going to stop answering you if you don't.
Java won't run because there a break statement outside a loop, but I have it inside a loop.
No, you have it inside an if
statement, which is not a loop. Even if it worked, break
would be useless there, the statement will be executed only once.
An if-else block is not a loop, that is your issue here. Your second error might be because x and y are not declared, at least not in the code you showed us.
at your line 59, there is break statement in the IF block. which doesn't make sense. Remove it and your program is good to go.
Thanks alee for your suggestion.
I move repaint(); method from the paint class to MyMouseListener class. That work perfectly and Java was repainting a image constantly,
Chapter 14 Java Programming Challenge number 2.
Write an applet that draws the house shown on the left in figure 14-32. When the user clicks
on the door or windows, they should close. The figure on the right shows the house with its
door and windows closed.
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.Graphics;
/**
*
* @author George Beazer
*/
public class Pigment extends JApplet
{
public int x = 0;
public int y = 0;
public static void main(String [] args)
{
}
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);
if (x > 100 && x < 175 && y < 155 && y > 75)
{
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, 154);
g.drawLine (440, 75, 440, 154);
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);
}
else if (x > 400 && x < 475 && y < 155 && y > 75)
{
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, 154);
g.drawLine (440, 75, 440, 154);
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);
}
else if (x > 240 && x < 360 && y < 400 && y > 200)
{
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, 154);
g.drawLine (440, 75, 440, 154);
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);
}
else
{
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);
}
}
private class MyMouseListener implements MouseListener
{
public void mouseEntered (MouseEvent e)
{
}
public void mouseExited(MouseEvent e)
{
}
public void mousePressed (MouseEvent e)
{
}
public void mouseReleased (MouseEvent e){}
public void mouseClicked(MouseEvent e)
{
x = e.getX();
y = e.getY();
showStatus( "Mouse at (" + x + "," + y + ")" );
repaint();
}
}
}
You can close this thread.
精彩评论