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

发布于 2024-10-19 02:03:32 字数 2716 浏览 2 评论 0原文

我有两个奇怪的错误

新错误是当我告诉 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

醉酒的小男人 2024-10-26 02:03:32

关于您的第一个问题,您在这里收到编译器错误的原因

if (x > 35)
{
    g.drawLine (35, 50, 570, 50);
    g.drawLine (35, 50, 250, 0);

    repaint();
    break;  
}

是:这个 break 语句实际上并不在循环中。在 Java 中,您可以打破 whilefordo...whileswitch 语句,但不是 if 语句。这没有什么特别好的理由——主要是历史原因——但编译器确实强制执行它。

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

您可以突破 switch 的原因是 Java 的 switch 语句基于 C 编程语言的 switch 版本,其中标签为“跌倒。”在此上下文中,break 表示“我已完成执行此特定标签中要执行的所有代码;请让我退出此语句。”有趣的是,您可以中断脱离开关,但不能继续

但是,您无法break 脱离if 语句。这没有任何高级原因,事实上,语言设计者可以很容易地允许这种行为意味着“停止执行 if 语句的这一部分”。我认为他们选择不这样做的原因是 if 语句在每个处理程序的末尾都有一种“隐式 break”。例如,如果您

if (condition()) {
    // A
} else {
    // B
}
// C

在执行 A 后编写 Then,控制流将立即跳转到 C,而不是跳转到 else 处理程序。

如果您想在 if 语句中间模拟 break,您可以执行如下操作:

if (condition()) {
     // code

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

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

希望这有帮助!

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!

羅雙樹 2024-10-26 02:03:32

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.

新人笑 2024-10-26 02:03:32

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

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

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.

無心 2024-10-26 02:03:32

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.

节枝 2024-10-26 02:03:32

在第 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.

苏辞 2024-10-26 02:03:32

谢谢阿丽的建议。

我移动 repaint();方法从paint类到MyMouseListener类。效果非常好,Java 不断地重新绘制图像,

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

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

门或窗时,它们应该关闭。右图显示的是

门窗紧闭的房屋。

/*
 * 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();
        }       
    }
}

您可以关闭此线程。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文