I've been trying to get a hang of this number averaging program, basically, it takes inputs from the user, and once -1 is inputted, it will average all those inputs. I'm struggling to find a way to give the average as it is giving strange answers as of yet.
Here is the code:
import java.util.Scanner;
public class LoopingDemo1 {
public static void main(String[] args) {
String input = "";
int num = 0;
int sum = 0;
Scanner in = new Scanner(System.in);
int z = -1;
int i = 0;
int counter = 0;
counter++;
while (i <= 0) {
System.out.println("Please input a number");
input = in.nextLine();
num = Integer.parseInt(input);
if (num == z) {
int average = sum / counter;
System.out.println("Average value is: " + average);
} else {
sum += num;
}
//what does this operator do? sum = sum + num
}
counter++;
}
}
I do apologize in advance if this is breaking any rules.
sum += num
is a shorthand forsum = sum + num
. As for the rest: please create a minimal reproducible example and give concrete expected and observed values.count
outside the loop, and because you do integer division.