import acm.program.*;
public class AverageList extends ConsoleProgram{
public static final int sentinel=-1;
public void run(){
println("the program averagelist");
println("enter value "+sentinel+" to stop the program");
double ave=0;
int sum=0;
int n=0;
while(true){
int vaule=readInt();
int i=n++;
sum+=vaule;
ave=sum/i;
if(vaule==sentinel)break;
}
println("the averaage is"+ave);
}
}
你之前的程序裡實在循環裡邊寫的int i = n++;所以每次循環的時候i都是從新定義的,並且你n++還是先復制給i的所以每次循環都是除的0,所以會報錯