簡體   English   中英

Java不會運行,因為在循環外有一個break語句,但是我在循環內有它

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

我有兩個奇怪的錯誤

新的錯誤是當我告訴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){}

    }

}

關於第一個問題,您在這里遇到編譯器錯誤的原因:

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 switch ,但不能continue

但是,你不能break一個的出來if語句。 這沒有高層的原因,實際上,語言設計人員可以很容易地使這種行為表示“停止執行if語句的這一部分”。 我認為他們選擇不這樣做的原因是, if語句在每個處理程序的末尾都有某種“隱式break ”。 例如,如果您寫

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

然后,在執行A ,控制流將立即將您跳至C ,而不是進入else處理程序。

如果要模擬if語句中間的break ,可以執行以下操作:

if (condition()) {
     // code

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

這里的想法是您運行if語句一段時間,然后使用第二條 if語句決定是否在循環中運行其余代碼。

希望這可以幫助!

if (x > 35) {...} 不是循環,則為語句。

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

while( x <= 35 ) {...}是一個循環。

do {...} while ( x <= 35 )是一個循環do {...} while ( x <= 35 )

帶上repaint()調用是多余的。

您確實需要去對先前問題“已接受”的答案單擊對勾標記,否則,人們將停止回答您

Java不會運行,因為在循環外有一個break語句,但是我在循環內有它。

不,您可以將其包含在if語句中,這不是循環。 即使它起作用了, break也沒有用,該語句將只執行一次。

if-else塊不是循環,這是您的問題。 您的第二個錯誤可能是因為沒有聲明x和y,至少在您顯示給我們的代碼中沒有聲明。

在第59行, IF塊中有break語句。 這沒有任何意義。 刪除它,您的程序就很好了。

感謝alee的建議。

我移動了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();
        }       
    }
}

您可以關閉該線程。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM