Когда я запускаю свою программу, консоль игнорирует первый целочисленный ввод и вместо этого добавляет 0 к sumOfPositive. Например, после запуска программы, если вход: 5, 6, 7, тогда sumOfPositive будет равен 13. Но если я изменю начальное значение sumOfPositive от 0 до слова 2, тогда sumOfPositive будет равен 15.Игнорирование инициализированного значения в переменной
Итак, как я проигнорировал инициализированное значение sumOfPositive и работал только с тем, что было зафиксировано на входе?
Мой код:
import java.util.Scanner;
public class Assignment2 {
public static void main(String[] args) {
Scanner myScanner = new Scanner(System.in);
int sumOfPositive = 0;
int sumOfOdd = 0;
int minInt = 0;
int numberOfInts = 0;
int eachIntEntered = myScanner.nextInt();
while (eachIntEntered != 0) {
// if the number entered is not 0, assign it to numberOfInt
eachIntEntered = myScanner.nextInt();
// if the number entered does not equal 0, add one to numberOfInt
// if the number entered is positive, add the number to sumOfPositive
if (eachIntEntered > 0) {
numberOfInts++;
sumOfPositive += eachIntEntered;
}
// if the number entered is odd, add the number to sumOfOdd
if (eachIntEntered % 2 != 0) {
sumOfOdd += eachIntEntered;
}
if (eachIntEntered < minInt) {
minInt = eachIntEntered;
}
} // end of while loop
System.out.println("The minimum integer is " + minInt);
System.out.println("The sum of the positive integers is " + sumOfPositive);
System.out.println("The sum of the odd integers is " + sumOfOdd);
System.out.println("The count of the positive integers in the sequence is " + numberOfInts);
} // end of main function
} // end of class
Первое число считывается перед циклом. Компьютер делает именно то, что вы ему сказали. – immibis
Что вы имеете в виду: 'игнорирует инициализированное значение sumOfPositive и работает только с тем, что было записано из ввода?'? –