這是我寫的程式:
但是寫出來出現一堆亂碼
為什麼?請大師指點,謝謝!!!
import java.util.Scanner;
public class Divisors {
public static void main (String [] args) {
/* this array will be filled with the divisors of a user-supplied number. */
long number;
long[] divisorArray;
Scanner scan = new Scanner(System.in);
System.out.println(" give me a long int and I'll "
+ "return an array of all its proper divisors > ");
number = scan.nextLong(); /* this is the number whose divisors we want to find and print */
Divisors div = new Divisors(); /* here we make an instance of the Divisors class*/
divisorArray = div.getDivisors(number);/* *** here we call a non-static method getDivisors . This method has as its argument the variable number we just read. getDivisors fills a local array with the divisors of number and returns this array. */;
System.out.println("the divisors of " + number + " are:");
/* *** here we print out all non-zero elements of divisorArray. Try to print at most 10 values per line (see the sample output above). */
System.out.println("\n "+divisorArray);
}// end main
public long[] getDivisors(long num) {
long[] divisorArray=new long[100];
int i=-1;
int j;
for(j=1;j
i++;
divisorArray[i]=j;
}
}
return divisorArray;
}
}
2007-02-01 20:55:50 · 1 個解答 · 發問者 小壁虎 1 in 電腦與網際網路 ➔ 程式設計
你的程式將計算的結果存到 divisorArray 這個 array 中,然後用
System.out.println("n " + divisoryArray) 的方式印出。這個方式會去
呼叫 divisoryArray 這個物件的 toString() 方法,由於 divisoryArray
是一個 Array 物件,它的 toString() 方法印出的是該物件在記憶體
中的位置,所以輸出才會是一堆隨機的英數字。建議的作法是用
一個 for 廻圈將其內容一個一個印出,請參考我的做法
for (int i = 0; i < divisorArray.length; i++) {
System.out.print(divisoryArray[i] + ", ");
if (i % 9 == 0) System.out.println(); // 10 results per line
}
2007-02-02 06:51:50 · answer #1 · answered by ? 7 · 0⤊ 0⤋