lon*_*rah 0 java applet loops return
我有两个奇怪的错误
新错误是当我告诉java绘制一个显示x和y坐标的字符串时,它不会.
public void paint (Graphics g)
{
super.paint (g);
//System.out.println ("Boolean: " + this.closeDoors);
g.drawString("("+x+","+y+")",x,y);
}
Run Code Online (Sandbox Code Playgroud)
如果要编译它,请链接到我的程序. 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){}
}
}
Run Code Online (Sandbox Code Playgroud)
关于你的第一个问题,你在这里得到编译器错误的原因:
if (x > 35)
{
g.drawLine (35, 50, 570, 50);
g.drawLine (35, 50, 250, 0);
repaint();
break;
}
Run Code Online (Sandbox Code Playgroud)
这个break陈述实际上是不是在循环中.在Java中,你可以打出来的while,for,do...while,和switch语句,而不是if语句.这没有特别好的理由 - 它主要是历史性的 - 但编译器确实强制执行它.
上述三个控制结构,for,while,和do...while被因为它们执行代码可能多次称为回路.break在这种情况下的声明是一种说法"请中止当前循环的执行;我不想再运行它." 相反的是continue,这意味着"请转到此循环的下一次迭代".
你可以摆脱a的原因switch是因为Java的switch陈述是基于C编程语言的switch标签"落伍" 的版本.在这个上下文中,break意思是"我已经完成了我想要在这个特定标签中执行的所有代码;请让我脱离这个声明." 有趣的是,你可以break出一个switch,但你不能continue.
但是,你不能break脱离if声明.这没有高级别的原因,实际上语言设计者可以很容易地允许这种行为意味着"停止执行if声明的这一部分".我认为他们选择不这样做的原因是if语句break在每个处理程序的末尾都有一种"隐含的".例如,如果你写
if (condition()) {
// A
} else {
// B
}
// C
Run Code Online (Sandbox Code Playgroud)
然后在执行之后A,控制流将立即跳转到您C,而不是落入else处理程序.
如果你想模拟一个语句break的中间部分if,你可以这样做:
if (condition()) {
// code
if (someOtherCondition()) {
// more code
}
}
Run Code Online (Sandbox Code Playgroud)
这里的想法是你运行if语句一段时间,然后决定使用第二个 if语句是否在循环中运行其余的代码.
希望这可以帮助!
小智 5
if (x > 35) {...}它不是一个循环它是一个声明.
for (int x = 0; x <= 35; x++) {...} 是一个循环.
while( x <= 35 ) {...} 是一个循环.
do {...} while ( x <= 35 ) 是一个循环.
就拿repaint()召唤出它是多余的.
你真的需要去点击你之前问题"接受"的答案上的CheckMark,如果你不这样做,人们就会停止回答你.