java程序运行时出错

时间:2016-06-09 00:20:18

标签: java

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.562583.5625

1 个答案:

答案 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);
        }
    }
}