import java.util.Scanner;
public class AvgTime {
public static void main( String[] args ) {
Scanner in = new Scanner(System.in);
final int SETINEL = 0;
double sum = 0;
int count;
int runnerID;
for(runnerID = in.nextInt() ; runnerID != 0 ; runnerID = in.nextInt())
{
sum = 0;
for(count = 1 ; count <= 8 ; count++)
{
double runnerTime = in.nextDouble();
sum += runnerTime;
}
double average = sum / count;
System.out.println("ID: " + runnerID + "Average: " + average);
}
}
}
尝试输入值
2346789
90
80.5
95
100
90
88.5
78
94.5
2347890
100
90
75.5
80
75.5
89
78.5
80
0
我得到了
ID: 2346789Average: 79.61111111111111
ID: 2347890Average: 74.27777777777777
我应该89.5625
和83.5625
答案 0 :(得分:1)
您在for
循环后添加分号,以使其以意外方式运行。删除它们。
你还必须修复内循环的问题,你只读了一次,我想它应该被读8次并添加。另外,请不要忘记为每位参赛者初始化sum
。
试试这个:
import java.util.Scanner;
public class AvgTime {
public static void main( String[] args ) {
Scanner in = new Scanner(System.in);
final int SETINEL = 0;
double sum = 0;
int count;
int runnerID;
for(runnerID = in.nextInt() ; runnerID != 0 ; runnerID = in.nextInt())
{
sum = 0;
for(count = 0 ; count < 8 ; count++)
{
double runnerTime = in.nextDouble();
sum += runnerTime;
}
double average = sum / count;
System.out.println("ID: " + runnerID + "Average: " + average);
}
}
}