find sum and average of n numbers

public class Solution {
    public static void main(String[] args) {
        Scanner ip = new Scanner(System.in);
        double input = 0;
        int count = 0;
        double sum = 0;
        while (input != -1) {
            System.out.print("Enter input:(-1 to stop): ");
            input = ip.nextDouble();
            if (input != -1) {
                sum = sum + input;
                ++count;
            }
        }
        System.out.println("Averager of " + count + " numbers is " + (sum / count) + " and its sum: " + sum);
        ip.close();
    }
}



OUTPUT:
Enter input:(-1 to stop): 10
Enter input:(-1 to stop): 20
Enter input:(-1 to stop): 30
Enter input:(-1 to stop): -1
Averager of 3 numbers is 20.0 and its sum: 60.0

猜你喜欢

转载自www.cnblogs.com/sea-stream/p/12013074.html
今日推荐