[英]Why isn't my for loop that assigns values to arrays working?
我希望程序輸出所有輸入的數字,但它只是一遍又一遍地輸出數字中的最后一位數字。
import java.util.Scanner;
public class Example9 {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
//asks user to input the first 9 digits of an ISBN
System.out.println("Enter first nine digits: ");
int firstNine = input.nextInt();
int[] digits = new int[9];
int digitsLeft = firstNine;
for(int i = digits.length - 1; i > 0; i--) {
int digit = firstNine % 10;
digitsLeft = (int) Math.floor(digitsLeft / 10);
digits[i] = digit;
System.out.println(digits[i]);
}
}
}
int digit = firstNine % 10;
一定是
int digit = digitsLeft % 10;
此外,如果i > 0
您將不會添加第一個數字。 它必須是i >= 0
。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.