2012-05-25 159 views
0

这或多或少是我的第一个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 

     } 
    } 
} 

回答

1

我想你的意思是这个

integer = Integer.parseInt(number); 

integer = Integer.parseInt(number2); 
+0

感谢您在分号上的提示,帮助清理了代码。并且@seth感谢那里的修复,修复了它。拧上变量:P – Nadim

4

值得指出的是,你有几个流浪分号在代码:

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

if (n==1); 
     ^HERE 

此外,你从来没有真正看过number2(第二个parseInt()调用看起来不正确)。