Java 不会运行,因为在循环外有一个 break 语句,但我在循环内有它

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5097138/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 09:24:36  来源:igfitidea点击:

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

javaloopsappletreturn

提问by lonesarah

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.

新错误是当我告诉 java 绘制一个显示 x 和 y 坐标的字符串时,它没有。

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

如果要编译它,请链接到我的程序。 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){}

    }

}

回答by templatetypedef

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 breakstatement is not actually in a loop. In Java, you can break out of while, for, do...while, and switchstatements, but not ifstatements. There's not a particularly good reason for this - it's mainly historical - but the compiler does indeed enforce it.

是不是这个break语句实际上不是在循环中。在Java中,你可以打破了whilefordo...while,和switch语句,而不是if语句。对此没有特别好的理由——主要是历史原因——但编译器确实强制执行了它。

Of the three aforementioned control structures, for, while, and do...whileare referred to as loops because they execute code potentially many times. The breakstatement 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."

在上述三个控制结构中forwhile、 和do...while被称为循环,因为它们可能会多次执行代码。break这种情况下的语句是一种表达“请中止当前循环的执行;我不想再运行它”的方式。它的反义词是continue,意思是“请转到这个循环的下一次迭代”。

The reason you can break out of a switchis because Java's switchstatement is based on the C programming language's version of switchin which labels are "fall-through." In this context, breakmeans "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 breakout of a switch, but you can't continue.

您可以跳出 a 的原因switch是因为 Java 的switch语句是基于 C 编程语言版本的switch,其中标签是“fall-through”。在这种情况下,break意思是“我已经完成了我想在这个特定标签中执行的所有代码;请让我退出这个声明。” 有趣的是,您可以break退出 a switch,但您不能continue

However, you cannot breakout of an ifstatement. 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 ifstatement." I think the reason they opted not to do this is that ifstatements have a sort of "implicit break" at the end of each handler. For example, if you write

但是,您不能break出于if声明。对此没有高层次的原因,事实上,语言设计者可以很容易地允许这种行为意味着“停止执行if语句的这一部分”。我认为他们选择不这样做的原因是if语句break在每个处理程序的末尾都有一种“隐式”。例如,如果你写

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 elsehandler.

然后在执行之后A,控制流将立即跳转到C,而不是落入else处理程序。

If you want to simulate a breakout of of the middle of an ifstatement, you could do something like this:

如果你想模拟一个语句break的中间if,你可以这样做:

if (condition()) {
     // code

     if (someOtherCondition()) {
          // more code
     }
}

The idea here is that you run the ifstatement for some time, then decide using a secondifstatement whether or not to run the rest of the code in the loop.

这里的想法是您运行该if语句一段时间,然后决定使用第二个if语句是否运行循环中的其余代码。

Hope this helps!

希望这可以帮助!

回答by templatetypedef

if (x > 35) {...}is nota loop it is a statement.

if (x > 35) {...}不是一个循环是一个声明。

for (int x = 0; x <= 35; x++) {...}is a loop.

for (int x = 0; x <= 35; x++) {...}是一个循环。

while( x <= 35 ) {...}is a loop.

while( x <= 35 ) {...}是一个循环。

do {...} while ( x <= 35 )is a loop.

do {...} while ( x <= 35 )是一个循环。

Take the repaint()call out it is redundant.

repaint()电话拿出来是多余的。

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.

你真的需要去点击你之前问题“接受”的答案上的复选标记,如果你不这样做,人们就会停止回答你

回答by nico

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

Java 不会运行,因为在循环外有一个 break 语句,但我在循环内有它。

No, you have it inside an ifstatement, which is not a loop. Even if it worked, breakwould be useless there, the statement will be executed only once.

不,你把它放在一个if语句中,它不是一个循环。即使它有效,break在那里也没用,该语句将只执行一次。

回答by Anthony Vallée-Dubois

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.

if-else 块不是循环,这是您的问题。您的第二个错误可能是因为 x 和 y 未声明,至少在您向我们展示的代码中没有声明。

回答by x.509

at your line 59, there is breakstatement in the IFblock. which doesn't make sense. Remove it and your program is good to go.

在第 59 行,IF块中有break语句。这没有意义。删除它,您的程序就可以运行了。

回答by lonesarah

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,

我移动 repaint(); 从paint类到MyMouseListener类的方法。这工作完美,Java 不断地重新绘制图像,

Chapter 14 Java Programming Challenge number 2.

第 14 章 Java 编程挑战第 2 号。

Write an applet that draws the house shown on the left in figure 14-32. When the user clicks

编写一个小程序,绘制如图 14-32 左侧所示的房子。当用户点击

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.

您可以关闭此线程。