在java程序中使用方法

时间:2013-09-23 20:50:02

标签: java methods printwriter

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

当我编译代码时,我得到一个错误说

  

文件:F:\ COMPSCI 12 \ java1.java [line:37]   错误:F:\ COMPSCI 12 \ java1.java:37:缺少return语句

但我已经有了回复声明。

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

没有方法的原始代码

    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

3 个答案:

答案 0 :(得分:4)

假设numpoints == 0。您的代码是否会达到退货声明?

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

Java认识到存在可能无法访问return语句的情况,并且就像没有返回语句一样。要解决这个问题,你应该在函数末尾有一个“default”return语句来处理没有输入循环的边缘情况。

  

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

这取决于您和代码的作用,但有一些指导原则:

  • 方法太长而无法理解?将其分解为几种方法。
  • 你在写“重复代码”吗?也许这应该采用一种方法。
  • 写入文件的东西是一个独立的操作单元。换句话说,与程序其余部分的逻辑分开。因此它应该作为自己的方法分开。

答案 1 :(得分:0)

方法错了。您将返回值声明为Double,但是您尝试返回双打数组。另外,在循环的第一次迭代期间将调用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
  }

答案 2 :(得分:0)

其他人指出了几件事。

我认为这里最重要的一般原则是separation of concerns。在您的特定情况下,在一个地方计算某些内容,并将数据保存到文件中是两个明显不同的问题。