簡體   English   中英

有人可以解釋一下這個while循環的結構嗎?

[英]Can someone please explain the structure of this while loop?

我需要知道為什么 sum=0 被初始化。 如果沒有必要,我需要知道原因。 謝謝。

最初的問題是:給定輸入正數 integer, output 每個數字在其自己的行上,從最右邊的數字開始。 例如:如果輸入為 935,則 output 為:5 3 9

import java.util.Scanner;

public class Main {
   public static void main(String[] args) {
      Scanner scnr = new Scanner(System.in);
      int number, sum=0, r;
      number = scnr.nextInt();
  
  while(number!=0) {
     r=number %10;
     System.out.println(r + "");
     number = number / 10;
  }
  /* Type your code here. */
}

sum = 0 不是必需的,因為您從不在程序中使用它,為什么要編寫一個您永遠不會使用的變量? 這就像買了一台電視機但從不使用它

暫無
暫無

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

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