2016-08-01 78 views
-3

我是編程新手,而且很難創建一個程序,該程序將從for循環中的數組收集的數據顯示到表中。我能夠收集數據,但無法存儲並在之後顯示。有什麼想法嗎?顯示錶格的循環

這是我寫

import java.util.Scanner; 

//Fahrenheit to Celsius converter 

public class CelsiusConversion 
{ 

    public static void Celsius(String[] args) 
    {//Open method 1 

     int num; 
     double [] temps; 
     double fahrenheit; 

     Scanner input = new Scanner(System.in); 

     System.out.println("Enter the amount of numbers you wish to average: "); 
     num = input.nextInt(); 

     while (num<1) 
     { 
      System.out.println("You did not enter a number greater than zero. Please enter a number greater than zero:"); 
      num = input.nextInt(); 
     } 

     temps = new double [num]; 

     for (int t = 0; t <num; t++) 
     { 
      System.out.println("Enter temperature " + (t+1) + " in Fahrenheit:"); 
      temps[t] = input.nextDouble(); 

      System.out.println("Please confirm the temperature in Fahrenheit"); 
      fahrenheit = input.nextDouble(); 

      double celsius = 5.0/9*(fahrenheit - 32); 

      System.out.println(fahrenheit + " in Celsius is " + celsius + "."); 

     } 

    }//Close method 1 


} 
+0

我想顯示的外部數據的循環,如果可能的 – Priice

+4

後的你已經嘗試過的代碼。並且請把它作爲編輯而不是評論上的問題。 – Julian

+1

請提供一個代碼示例,並詳細解釋什麼是不工作,你想要和你得到的錯誤 –

回答

0

沒有任何的示例代碼很難給出建議,但這裏是一個陰謀和一個建議:

我注意到你包括標記「陣列」,做你完全理解數組?你在使用數組嗎?如果你確保你在循環的每次迭代中初始化數組的不同部分(這被稱爲「遍歷」數組)。如果你不這樣做,你的數組只會保存你在循環的最後一次迭代中輸入到數組中的最後一個值。

另外,用System.out直接顯示數組是有點兒不合適的。相反,您可以再次穿過陣列來製作字符串,也可以使用Arrays.toString(array)

例如,第一個代碼顯示1, 2, 3, 4。下面的代碼顯示[1, 2, 3, 4]

int[] num = {1, 2, 3, 4}; 

String print = ""; 

for (int hold : num) 
{ 
    print += hold + ", "; 
} 

System.out.println(print + "\b\b"); 

下一頁碼

int[] num = {1, 2, 3, 4}; 

System.out.println(Arrays.toString(num));