jeudi 26 août 2021

Trying to find the smallest integer. Code works fine for some input, but not all

My first if statement is what i'm trying to fix. Code is supposed to stop when it detects a 0 from input. Obviously this isn't the full code, but ask if you need the full thing. If you notice any other flaws, please let me know.

Side note, I do not want to use arrays.

        int min = 0;
        int maxOddNum = 0;
        int negativeInt = 0;
        int sumEvenInt = 0;
        int num;
        
        do {
            num = scanner.nextInt();    // reads integers from user input
            
            if(num < min)    // finds the smallest integer
                min = num;
            
            if(num % 2 != 0 && num > maxOddNum)    // finds the largest odd integer
                maxOddNum = num;
            
            if(num < 0)    // finds the number of negative integers
                negativeInt++;
            
            if(num % 2 == 0)    // finds the sum of even integers
                sumEvenInt += num;
        }
        
        while(num != 0);
        

Input: 6 8 10 20 0 -1 13

Expected Output: The minimum integer is 6 The largest odd integer in the sequence is 0 The count of negative integers in the sequence is 0 The sum of even integers is 44

Aucun commentaire:

Enregistrer un commentaire