虽然循环不能正常工作?

时间:2012-05-25 16:00:52

标签: java if-statement applet while-loop

这或多或少是我的第一个java项目。我无法使while循环正常工作。它似乎循环的次数与我为第一个选项输入的整数一样多。但我希望它能用于我的第二个。我的电脑老师根本不是很有帮助。

import java.awt.*;
public class Summative extends JApplet
{
    int n; //first choice variable
    int t; //time variable
    int integer; //integer input
    int x=0; //count variable
    int y=50; //test

    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);
        { 
        }
        do
        {
            String number2 = JOptionPane.showInputDialog("How many times would you like to loop?");
            integer = Integer.parseInt(number);
        }while (integer<0 || integer>99999);


        while (x < integer)
        {   
            g.drawString("hi", 200, y); 

            x+=1;
            y = y+40; //test

        }
    }
}

2 个答案:

答案 0 :(得分:4)

值得指出的是,您的代码中有几个分散的分号:

while (n<0 || n>2);
                  ^ HERE

if (n==1);
         ^ HERE

此外,您实际上从未查看number2(第二个parseInt()调用看起来不正确。)

答案 1 :(得分:1)

我认为你的意思是

integer = Integer.parseInt(number);

integer = Integer.parseInt(number2);