Write a program that asks the user for input until the user inputs 0. After this the program prints the amount of numbers inputted and the sum of the numbers. The number zero does not need to be added to the sum, but adding it does not change the results.
import java.util.Scanner; public class AverageOfNumbers { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); int count=0; int sum=0; while(true){ System.out.println("Give a number: "); int number=Integer.valueOf(scanner.nextLine()); if(number==0){ break; } else if(number!=0){ count++; sum +=number; } } System.out.println("sum:"+sum); double average = sum/(double)count; System.out.println("Sum=" + sum + ", Average=" + average); } }
https://stackoverflow.com/questions/65785306/i-am-having-problems-with-my-code-its-not-able-to-calculate-precise-average-c January 19, 2021 at 12:00PM
没有评论:
发表评论