![](/img/trans.png)
[英]Will Java notice when a loop won't change anything, or should I use break statement?
[英]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中,您可以打破while
, for
, do...while
和switch
語句,但不能脫離if
語句。 沒有一個特別好的理由-這主要是歷史原因-但編譯器確實可以執行它。
在上述三個控制結構中, for
, while
和do...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.