在 while 循环中发现困难。我不确定如何开始调用和/或读取生成文件中的每个数字,以便确定最小和最大数字以及总和。另外,如果可能的话,有人可以解释如何编写代码,以便如果任何数字是连续的,它就会计算多少次。
PrintWriter prw = new PrintWriter("results.txt");
int num, largest, smallest, total = 0, count = 0;
int programnumber = 6;
double average = 0;
PrintWriter prw1= new PrintWriter(new FileWriter("randomdata.txt"));
Random generator = new Random();
for (int i = 0; i < 1001; i++){
num = generator.nextInt(500); //Will this generate a file w/ 500 num?
prw1.write(num + "\n");
}
prw1.close();
largest = 0;
smallest = 9999;
while (prw1.nextInt()){ //what call statement do I use?
num = (prw1.nextInt()); //unsure how to begin reading numbers
if (num > largest){
largest = num;
}
if (num < smallest){
smallest = num;
}
total += num;
count++;
}
average = (total / total);