我想製作一個程序,它需要一個三位數的數字並將其分解爲數字。我沒有編譯或運行問題,但最終輸出給我0,0和0.用數組編號的數字程序
我對數組並不熟悉,所以我只是嘗試不同的東西。請讓我知道問題是什麼,或者有什麼我可以做的來改進我的代碼。
import java.util.Scanner;
public class numToDig{
public static void main(String[] args) {
Scanner s=new Scanner(System.in);
System.out.print("enter a three digit number: ");
int n=s.nextInt();
int arr[]=new int[n];
System.out.println("The digits are: " + arr[0] + ", " + arr[1] + ", and " + arr[2]);
}
}
的問題是,你初始化數組,但不能與數字填充。作爲int(原始)的一個數組,初始化爲ceros –
請參閱文檔教程以獲取有關數組處理的幫助= https://docs.oracle.com/javase/tutorial/java/nutsandbolts/arrays.html – mjhouseman
「我不是所以對數組非常熟悉,所以我只是在嘗試不同的東西。「熟悉數組然後,不要在黑暗中行走:https://docs.oracle.com/javase/tutorial/java/nutsandbolts/arrays.html。 'new int [n];'不會做你認爲它做的事。對於這個任務,你可能想研究整數除法和模運算符。 – Pshemo