0

i'm doing a exercise, where i should to sum the numbers until that the user put zero, then to print at console, the average of the numbers, the problem is that when i use the variable named "result" do not divide the operation that i put

My code:

Scanner scanner = new Scanner(System.in);
        int number = 0;
        int counter = 0;
        
        do {
            System.out.println("Digit the numbers: ");
            number = scanner.nextInt();
            number += number;
            counter++;
        }while(number > 0);
        
        int result = number / counter;
        
        System.out.println("the average of the numbers is -> "+ result);

  • You need a separate variable to hold the sum. The way you are currently doing it, each time around the loop you are clobbering the sum. (And you are not summing correctly either ... related problem.) – Stephen C Sep 16 '21 at 04:29
  • Your problem is that you're trying to use the same variable for the number you entered, and for the total. Make a new variable called `total` and write `total += number;` – Dawood ibn Kareem Sep 16 '21 at 04:30

0 Answers0