亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

為什么我的哨兵值不起作用,我可以輸入無限的整數但什么也得不到?

為什么我的哨兵值不起作用,我可以輸入無限的整數但什么也得不到?

慕蓋茨4494581 2022-06-04 15:41:39
所以我正在嘗試編寫一個標記值為 0 的代碼。它提示用戶輸入數字,并找到所有輸入數字的正負和總和。我不明白為什么在我輸入標記值 0 后它不打印任何東西。我的 while 循環有問題嗎?謝謝你 !我嘗試了很多東西,但仍然看不到原因。package Csc220Study;import java.util.Scanner;public class S51 {public static void main(String [] args){    System.out.println(" enter an integer , the input ends if it is 0:");    Scanner input = new Scanner(System.in);    int sum =0;    int positives=0;    int negatives=0;    int count=0;    int x = input.nextInt();    while(x!=0){        sum=+x;        if(x<0){            negatives++;        }else if(x>0){            positives++;        }        count++;    }        System.out.println("the sum is :"+sum);        System.out.println("positive number here :"+positives);        System.out.println("negative numbers are :"+negatives);
查看完整描述

1 回答

?
烙印99

TA貢獻1829條經驗 獲得超13個贊

將您的 while 循環部分更改為:


int x = input.nextInt();

while(x!=0){

    sum+=x;

    if(x<0){

        negatives++;

    }else if(x>0){

        positives++;

    }

    count++;

    x = input.nextInt();

}

當您輸入一個非零整數時,它處于無限循環中,因為 x 的值永遠不會改變。


查看完整回答
反對 回復 2022-06-04
  • 1 回答
  • 0 關注
  • 125 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號