public class exec
{
public static void main( String args[] ) throws Exception
{
int amount=0;
int counter=0;
while(true)
{
int i=Integer.parseInt(javax.swing.JOptionPane.showInputDialog("Please input the number(end with zero)"));
if(i==0)
break;
amount=amount+i;
counter++;
}
String str=String.valueOf((double)amount/counter);
javax.swing.JOptionPane.showMessageDialog(null,str);
System.exit(0);
}
}
对于退出循环的比较可以直接使用字符串比较,方法是利用String变量中的equals方法,而不是 s=="0"
public class exec
{
public static void main( String args[] ) throws Exception
{
int amount=0;
int counter=0;
while(true)
{
String s=javax.swing.JOptionPane.showInputDialog("Please input the number(end with zero)");
if(s.equals("0"))
break;
int i=Integer.parseInt(s);
amount=amount+i;
counter++;
}
String str=String.valueOf((double)amount/counter);
javax.swing.JOptionPane.showMessageDialog(null,str);
System.exit(0);
}
}
[此贴子已经被作者于2010-12-12 07:37:48编辑过]