Java不会运行,因为循环外有一个break语句,但我把它放在循环内
我有两个奇怪的错误
新错误是当我告诉 java 绘制一个显示 x 和 y 坐标的字符串时,它没有。
public void paint (Graphics g)
{
super.paint (g);
//System.out.println ("Boolean: " + this.closeDoors);
g.drawString("("+x+","+y+")",x,y);
}
如果您要编译它,请链接到我的程序。 http://hotfile.com/dl/107032853/c81d927/Pigment.java.html
这是我的完整程序
/*
* 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(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){}
}
}
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(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){}
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
关于您的第一个问题,您在这里收到编译器错误的原因
是:这个
break
语句实际上并不在循环中。在 Java 中,您可以打破while
、for
、do...while
和switch
语句,但不是if
语句。这没有什么特别好的理由——主要是历史原因——但编译器确实强制执行它。在上述三个控制结构中,
for
、while
和do...while
被称为循环,因为它们可能多次执行代码。本例中的break
语句是一种表达“请中止当前循环的执行;我不想再运行它”的方式。它的反义词是continue
,意思是“请转到该循环的下一次迭代”。您可以突破
switch
的原因是 Java 的switch
语句基于 C 编程语言的switch
版本,其中标签为“跌倒。”在此上下文中,break
表示“我已完成执行此特定标签中要执行的所有代码;请让我退出此语句。”有趣的是,您可以中断
脱离开关
,但不能继续
。但是,您无法
break
脱离if
语句。这没有任何高级原因,事实上,语言设计者可以很容易地允许这种行为意味着“停止执行if
语句的这一部分”。我认为他们选择不这样做的原因是if
语句在每个处理程序的末尾都有一种“隐式break
”。例如,如果您在执行
A
后编写 Then,控制流将立即跳转到C
,而不是跳转到else
处理程序。如果您想在
if
语句中间模拟break
,您可以执行如下操作:这里的想法是运行
if
语句一段时间,然后使用第二if
语句决定是否运行循环中的其余代码。希望这有帮助!
With regards to your first question, the reason that you're getting a compiler error here:
Is that this
break
statement is not actually in a loop. In Java, you can break out ofwhile
,for
,do...while
, andswitch
statements, but notif
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
, anddo...while
are referred to as loops because they execute code potentially many times. Thebreak
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 iscontinue
, which means "please go to the next iteration of this loop."The reason you can break out of a
switch
is because Java'sswitch
statement is based on the C programming language's version ofswitch
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 canbreak
out of aswitch
, but you can'tcontinue
.However, you cannot
break
out of anif
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 theif
statement." I think the reason they opted not to do this is thatif
statements have a sort of "implicitbreak
" at the end of each handler. For example, if you writeThen after executing
A
, the control flow will immediately jump you toC
, rather than falling through to theelse
handler.If you want to simulate a
break
out of of the middle of anif
statement, you could do something like this:The idea here is that you run the
if
statement for some time, then decide using a secondif
statement whether or not to run the rest of the code in the loop.Hope this helps!
if (x > 35) {...}
不是循环,而是语句。for (int x = 0; x <= 35; x++) {...}
是一个循环。while( x <= 35 ) {...}
是一个循环。do {...} while ( x <= 35 )
是一个循环。调用
repaint()
会发现它是多余的。您确实需要点击之前问题的“已接受”答案上的复选标记,否则人们将停止回答您。
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.
不,你把它放在
if
语句中,这不是循环。即使它有效,break
在那里也是无用的,该语句只会执行一次。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.if-else 块不是循环,这是您的问题。您的第二个错误可能是因为 x 和 y 未声明,至少在您向我们展示的代码中未声明。
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.
在第 59 行,IF 块中有 break 语句。这没有道理。删除它,您的程序就可以正常运行了。
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.
谢谢阿丽的建议。
我移动 repaint();方法从paint类到MyMouseListener类。效果非常好,Java 不断地重新绘制图像,
第 14 章 Java 编程挑战赛第 2 号。
编写一个小程序来绘制图 14-32 左侧所示的房子。当用户点击
门或窗时,它们应该关闭。右图显示的是
门窗紧闭的房屋。
您可以关闭此线程。
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.
You can close this thread.