簡體   English   中英

計數器變量在我的 do while 循環中沒有正常工作

[英]Counter variable is not working as it should in my do while loop

我不明白為什么正數的計數器變量沒有正確的數字? 我輸入四個正面和三個負面,這應該是正確的輸出。 0 不計入正數。 我也嘗試過前綴增量而不是后綴增量,但這也無濟於事。 我是新手,處於非常初級的水平。

這是我的輸入和輸出:

Enter an integer, the input end if it is 0: 4
Enter an integer, the input end if it is 0: 3
Enter an integer, the input end if it is 0: 2
Enter an integer, the input end if it is 0: 1
Enter an integer, the input end if it is 0: -4
Enter an integer, the input end if it is 0: -3
Enter an integer, the input end if it is 0: -2
Enter an integer, the input end if it is 0: 0
num of postives 3 and num of negatives 3 <----- output wrong
import java.util.Scanner;

public class App {
    public static void main(String[] args) throws Exception {
        /**
         * my pesducode
         * enter an integer, input ends if 0: 
         * while input != 0
         *     if input > 0
         *         postives = postiveCount++
         *     else
         *         negatives = negativeCount++
         *     total+= input
         *  end loop
         * totalNumsEnter = postive + negatives
         * avg = total/ totalNumsEnter
         */
        Scanner input = new Scanner(System.in);
        int num = 0;
        int postiveCount = 0;
        int postives = 0;
        int negatives = 0;
        int negativeCount = 0;
        do{
            System.out.print("Enter an integer, the input end if it is 0: ");
            num = input.nextInt();
            
            if(num > 0){
               postives = postiveCount++;
            }
            else{
                negatives = negativeCount++;
            }
        }while( num != 0);
        System.out.printf("num of postives %d and num of negatives %d", postives,negatives);
        input.close();
    }
}

將 else 語句更改為以下語句將忽略 0。

        Scanner input = new Scanner(System.in);
        int num = 0;
        int positiveCount = 0;
        int negativeCount = 0;
        do{
            System.out.print("Enter an integer, the input end if it is 0: ");
            num = input.nextInt();
            
            if(num > 0){
               positiveCount++;
            }
            else if (num < 0){
                negativeCount++;
            }
        }while( num != 0);
        System.out.printf("num of positives %d and num of negatives %d", positiveCount, negativeCount);
        input.close();

另一個選項(盡管它使用嵌套的 if)如下:

        Scanner input = new Scanner(System.in);
        int num = 0;
        int positiveCount = 0;
        int negativeCount = 0;
        do{
            System.out.print("Enter an integer, the input end if it is 0: ");
            num = input.nextInt();
            
            if(num >= 0){
               if (num != 0) {
                 positiveCount++;
               }
            }
            else{
                negativeCount++;
            }
        }while( num != 0);
        System.out.printf("num of positives %d and num of negatives %d", positiveCount, negativeCount);
        input.close();

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM