0

在将我的程序划分为方法(特别是主要方法和执行所有计算的另一种方法等)时,我遇到了麻烦。我不确定划分现有代码以创建新方法的正确方法。我的程序也写入文件。

当我编译代码时,我收到一条错误消息

文件:F:\COMPSCI 12\java1.java [行:37] 错误:F:\COMPSCI 12\java1.java:37:缺少返回语句

但我已经有一个退货声明。

我是否正确使用了这些方法?或者有什么问题?谢谢

没有方法的原始代码

    import java.io.*;

public class java1
{
  public static void main (String [] args) throws IOException
  {
    //int variables are declared
    int numpoints = 100, dimension = 2, length = 100;//numpoints is set to 100, dimension is set to 2, length is set to 100

    PrintWriter fileOut = new PrintWriter (new FileWriter ("arrayNumPoints.txt"));

    //arays are declared/initialized
    double [] lengthscale = new double [dimension];
    double [][] locations = new double [numpoints][dimension];

    for (int a = 0; a < dimension; a++){//for loop runs while a is less than dimension
      lengthscale[a] = length;//stores array
    }//end for loop

    for (int x=0; x < numpoints; x++){//for loop runs while x is less than numpoints
      for (int y=0; y < dimension; y++){//nested for loop runs while y is less than dimension
        locations [x][y]= (2 * Math.random() - 1) * lengthscale[y];//creates the range and choses random point within it

        fileOut.println( locations[x][y] + ", ");//prints out coordinate

      }//end nested for loop
    }//end for loop

    fileOut.close ();
  }//end main method
}//end cass

相同的代码但使用方法

 import java.io.*;

public class J4_2_MultiDimensionalArray7
{
  public static void main (String [] args) throws IOException
  {
    int numpoints = 100, dimension = 2, length = 100;//numpoints is set to 100, dimension is set to 2, length is set to 100

    //arrays are initializewd and declared
    double [] lengthscale = new double [dimension];
    double [][] locations = new double [numpoints][dimension];

    PrintWriter fileOut = new PrintWriter (new FileWriter ("arrayNumPoints.txt"));


    for(int m=0; m <length; m++){//for loop
      fileOut.println(java.util.Arrays.toString(locations[m]) + ", ");
    }
  }//end main

    public static Double writefile(Double locations[][], Double lengthscale[], int dimension, int numpoints, Double length)throws IOException
    {


    for (int a = 0; a < dimension; a++){//for loop runs while a is less than dimension
      lengthscale[a] = length;//stores array
    }//end for loop

    for (int x=0; x < numpoints; x++){//for loop runs while x is less than numpoints
      for (int y=0; y < dimension; y++){//nested for loop runs while y is less than dimension
        locations [x][y]= (2 * Math.random() - 1) * lengthscale[y];//creates the range and choses random point within it

      return locations[x][y];//returns the value of locations
      }//end nested for loop

    }//end for loop

    fileOut.close ();//close file
  }//end writefile methos
}//end cass
4

3 回答 3

4

假设numpoints == 0. 你的代码会到达 return 语句吗?

在另一种情况下,如果您的函数确实返回,是否会fileOut.close();被调用?

Java 认识到存在可能无法到达 return 语句的情况,并表现得好像您没有返回语句一样。要解决此问题,您应该在函数末尾有一个“默认”返回语句,以处理未进入循环的边缘情况。

我不确定划分现有代码以创建新方法的正确方法。

这真的取决于你和代码在做什么,但有一些指导方针:

  • 方法太长无法理解?将其分解为几种方法。
  • 你在写“重复代码”吗?也许这应该采用一种方法。
  • 像写入文件这样的东西是一个离散的操作单元。换句话说,与程序其余部分的逻辑分开。所以它应该作为自己的方法分开。
  • 等等。
于 2013-09-23T20:56:51.003 回答
0

方法不对。您将返回值声明为 Double,但是您试图返回一个 Doubles 数组。另外,在循环的第一次迭代期间将调用 return 语句,因此它会停在那里。

public static Double writefile(Double locations[][], Double lengthscale[], int dimension, int numpoints, Double length)throws IOException
    {   

    for (int x=0; x < numpoints; x++){
      for (int y=0; y < dimension; y++){
        locations [x][y]= (2 * Math.random() - 1) * lengthscale[y];

        return locations[x][y];  <------------ this would be called in the first iteration;
      }//end nested for loop

    }//end for loop

    fileOut.close ();//close file
  }
于 2013-09-23T20:55:58.707 回答
0

其他人指出了几件事。

我认为这里最重要的一般原则是关注点分离。在您的特定情况下,在一个地方计算一些东西,并将数据保存到一个文件中是两个不同的、明确的关注点。

于 2013-09-23T20:59:55.697 回答