【发布时间】:2012-05-31 04:33:08
【问题描述】:
在用户输入他们的选择后,程序似乎再次提供相同的选择,然后完全冻结。它只设置在那里给他们一次选项(我相信),然后按照下面的选择。相反,它似乎恢复到顶部,然后由于某种奇怪的原因冻结。尝试了一段时间,但无济于事。
public void init() //Initialize method {
setSize(1000, 800); //Set size
Container c = getContentPane();
c.setBackground(Color.GREEN); //Set background
}
public void paint(Graphics g) {
super.paint(g); //Start paint method
g.setFont(new Font("Veranda", Font.PLAIN, 20));
g.setColor(Color.BLACK);
g.drawString("Hello", 250, 25); //top display message
String number = JOptionPane.showInputDialog("Would you like a custom loop count or an infinite? 1. Custom 2. Infinite"); //test choice
n = Integer.parseInt(number);
while (n>0 || n<2)
if (n==1) {
String number2 = JOptionPane.showInputDialog("How many times would you like to loop?");
integer = Integer.parseInt(number2);
while (integer>0)
while (x < integer) {
g.drawString("hi", 200, y);
x+=1;
y = y+40; //test
}//end while
}//end if
else if (n==2) {
while (true); //im aware this is an endless loop. Can't find anything more stable for an endless one, so its just there for now..
}//end if
else;
g.drawString("Please pick a valid choice", 200, 200);
}//end paint
}//end
【问题讨论】:
-
添加一个标有 Java 的标签,以便知道它的人可以提供帮助——这样您的代码就会被着色。
-
while (integer > 0)看起来非常可疑。按照 Java 的解析规则,它包含一个语句——下一个 wwile 循环。 -
while (true)是一个无限循环:while (false)什么都不做,但不用说它令人困惑! -
while (true);做某事 - 它永远不会停止并破坏您的程序。
标签: java loops input while-loop freeze