2016-08-01 50 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));