Следующий код выводит сумму, среднее значение, количество положительных/отрицательных чисел, количество всех чисел правильно, когда запускается первый раз. Так как он петли, следовательно, выход остается на консоли, чтобы пользователь снова вводил числа. В это время только сумма показывает правильный результат, другие значения удваиваются. Пожалуйста, помогите мне в фиксации петли. Благодаря!JAVA do-while Loop удваивает выходное значение
public class Test {
public static void main(String[] args) {
long n;
int count=0;
float average;
int positive=0;
int negative =0;
Scanner in = new Scanner(System.in);
do {
System.out.print("Enter a positive or negative integers: ");
n = in.nextLong();
if (n == 0){
System.out.println("Integers you've entered is invalid. Please re-launch the Program.");
}
else
{
int sum=0;
do
{
//Find sum of the integers entered.
sum += n %10;
n /= 10;
//Count number of integers entered.
count++;
//Find average of the numbers
average = sum/count;
//Find a count of positive and negative numbers.
if(n < negative){
negative++;
}
else{
positive++;
}
} while (n != 0);
n = sum;
System.out.println("The Sum of the numbers: " + sum);
System.out.println("The Average of the numbers: " + average);
System.out.println("Positive numbers are: " + positive);
System.out.println("Negative numbers are: " + negative);
System.out.println("The count of all numbers: " +count);
}
} while(n != 0);
}
}
Какая разница вы видите в объявлении 'sum' и других переменных, которые в два раза? –