2017-04-21 52 views
0

使用Java,我應該創建一個存儲編號0,1的平方,2 10的元素的程序。方形陣列方案

我已經創建了顯示數字及其平方碼的一部分,但該程序直接進打倒所有的數字,不看組織。有人可以幫我寫出來喜歡這樣,而不是:

數:0平方:0

號:1的正方形:1

數:2平方:4

代碼

public static void main(String[] args) { 
    int[] temp = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9}; 

    for (int value : temp) { 
     System.out.println(value); 
    } 

    for (int i = 0; i < temp.length; i++) { 
     temp[i] = (int) Math.pow(temp[i], 2); 
    } 

    for (int value : temp) { 
     System.out.println(value); 
    } 
} 
+0

[絃樂爪哇輸出格式(http://stackoverflow.com/questions/4418308/java-output-formatting-for-strings ) –

+0

如果這是應該使用'ArrayList',哪裏是它的使用?還是你沒有真的意味着'ArrayList'? – KevinO

回答

1

你只需要一個循環是這樣的:

public static void main(String[] args) { 
    int[] temp = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9}; 

    for (int i = 0; i < temp.length; i++) { 
     System.out.println(temp[i] + "\t" + (int)Math.pow(temp[i], 2)); 
    } 
} 

輸出

0 0 
1 1 
2 4 
3 9 
4 16 
5 25 
6 36 
7 49 
8 64 
9 81 

如果你想存儲在一個ArrayList你的結果,你可以使用:

List<int[]> array = new ArrayList<>();//create a List which take an array of int 

int arr[] = new int[2];//create a temporary array of 2 elements 

for (int i = 0; i < temp.length; i++) { 
    System.out.println("Number: " + temp[i] + " \tSquare: " + (int) Math.pow(temp[i], 2)); 
    arr[0] = temp[i];//add your number to your array pos 1 
    arr[1] = (int) Math.pow(temp[i], 2);//add the power to the 2ed position 
    array.add(arr);//add your array to your list 

} 
+0

我相信你可以做'INT [] TEMP = INT新[10];'然後不輸出'臨時[I]'只是輸出'i'。我也傾向於在輸出中使用String.format(「%2d \ t%3f」)來對齊數字。 – KevinO

+0

@KevinO在這一點上,因爲它不需要陣列都可以用一個for循環完成。 – Romoku

+0

OP的問題需要把結果放在一個數組中。 – KevinO

0

試試這個:

public static void main(String[] args) { 
    int[] temp = {0, 1,2,3,4,5,6,7,8,9}; 

    for (int value : temp) { 
     System.out.println("number : "value); 
    } 

    for (int i = 0; i < temp.length; i++) { 
    temp[i] = (int) Math.pow(temp[i], 2); 
    } 

    for (int value : temp) { 
    System.out.println(" square : " + value + "\n"); 
    } 
} 
0
public static void main(String[] args) { 
    int[] temp = {0, 1,2,3,4,5,6,7,8,9}; 

    // here you are printing all your numbers in the array, so the output will be: 
    // 0 
    // 1 
    // ... 
    // 9 
    for (int value : temp) { 
     System.out.println(value); 
    } 


    // after that, you calculate and store them in the same array; so your array now look like this: 
    // [0,1,4,9,...,81] 
    for (int i = 0; i < temp.length; i++) { 
     temp[i] = (int) Math.pow(temp[i], 2); 
    } 


    // here you are printing again your array 
    // 0 
    // 1 
    // 4 
    // ... 
    // 81 
    for (int value : temp) { 
     System.out.println(value); 
    } 
} 

得到你想要的結果,你必須在打印之前的一切來計算數...一個選項將創建另一個數組中的第一個for循環

int[] square = new int[9]; 

計算,並將結果保存在廣場陣列,並打印出來,這樣的事情:

for (int i = 0; i < temp.length; i++) { 
    square[i] = (int) Math.pow(temp[i],2); 
    System.out.println("number " + temp[i] + " square: " + square[i]); 
} 
0

假設其中提到的ArrayList是正確的(在OP的例子只有一個數組)原來的問題,並假設結果應該被存儲在數組中(每個問題),然後下面的工作:

public static void main(String[] args) 
{ 
    // instantiate ArrayList 
    List<Double> array = new ArrayList<>(); 

    // load the values 
    for (int i = 0; i < 10; ++i) { 
    array.add(i, Math.pow(i, 2)); 
    } 

    // output 
    for (int i = 0; i < array.size(); ++i) { 
    System.out.println(String.format("%2d\t%3.0f", i, array.get(i))); 
    } 
} 

輸出:

0  0 
1  1 
2  4 
3  9 
4 16 
5 25 
6 36 
7 49 
8 64 
9 81