简体   繁体   English

阵列打印不正确

[英]Array not printing correctly

I am working on a program in which I am reading a list of Celsius temperatures from a file, loading them into an array, and eventually loading another array with the corresponding Fahrenheit temperatures for each Celsius temperature. 我正在研究一个程序,在这个程序中,我从一个文件中读取摄氏温度列表,将它们加载到一个数组中,最后在每个摄氏温度下加载另一个具有相应华氏温度的数组。

When attempting to print the fahrArray, rather than displaying actual temperatures which were converted to Fahrenheit to Celsius, I am given this 当试图打印fahrArray,而不是显示转换为华氏温度到摄氏温度的实际温度时,我得到了这个

[D@3d3b10b1>

Here is the code that I have so far. 这是我到目前为止的代码。

import java.util.Scanner;
import java.io.*;

public class BonusLab {
  public static void main(String[] args) throws IOException {

    createdBy();

    double[] celArray = loadCelArray();

    printArray(celArray);

    double[] fahrArray = loadFahrArray(celArray);

  } // end main

  public static void createdBy() {
    System.out.println("Program created by-Beth Tanner");
  } // end createdBy

  public static double[] loadCelArray() throws IOException {
    Scanner fin = new Scanner(new File("weather.txt"));

    int x = fin.nextInt();

    double[] celArray= new double[x];

    for(int i = 0; i < celArray.length; i++)
      celArray[i] = fin.nextDouble();

    return celArray;
  } // end loadCelArray

  public static void printArray(double[] celArray) {
    for(int i = 0; i < celArray.length; i++)
    System.out.print(celArray[i] + " ");
  } // end printArray

  public static double[] loadFahrArray(double[] celArray) throws      IOException {
    Scanner fin = new Scanner(new File("weather.txt"));

double celsius = 

int x = fin.nextInt();

double[] fahrArray = new double[x];
for(int i = 0; i < fahrArray.length; i++) 
  fahrArray[i] = (9 / 5) * celsius + 32;

return fahrArray;
  } // end loadFahrArray

} // end class

Any help or suggestions would be greatly appreciated. 任何帮助或建议将不胜感激。

If I understood your code right, I think you should change this code 如果我理解你的代码是正确的,我认为你应该改变这段代码

for(int i = 0; i < fahrArray.length; i++) 
    fahrArray[i] = (9 / 5) * celsius + 32;

to

for(int i = 0; i < fahrArray.length; i++) 
    fahrArray[i] = (9 / 5.0) * celArray[i] + 32;

You are trying to access celArray[i] at a position where i is not defined. 您正尝试在未定义i的位置访问celArray [i]。 In the for-loop i is defined and should be at the same position. 在for-loop中,i被定义并且应该处于相同的位置。

Another soluion is, that you can leave out the second scanning of the file, because u have already loaded the values. 另一个解决方案是,您可以省略文件的第二次扫描,因为您已经加载了值。 So it will be enough to loop through calArray. 所以循环通过calArray就足够了。

Edit: user3008950 now provided the second possibility I wrote about. 编辑:user3008950现在提供了我写的第二种可能性。

public static double[] loadFahrArray(double[] celArray){
    double[] result=null;
    if(celArray!=null){
        result=new double[celArray.length];
        for(int i = 0; i < celArray.length; i++){ 
            result[i] = (9 / 5.0) * celArray[i] + 32;
        }
    }
    return result;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM