2013-09-23 27 views
0

当我将程序划分为方法(特别是主方法和执行所有计算的另一种方法等)时,我遇到了麻烦。我不知道如何划分我现有的代码来创建一个新的方法。我的程序也写入一个文件。在java程序中使用方法

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

File: F:\COMPSCI 12\java1.java [line: 37] Error: F:\COMPSCI 12\java1.java:37: missing return statement

但我已经有一个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 
+1

您需要为代码可能采取的每条可能路径提供返回值 – porfiriopartida

回答

4

假设numpoints == 0。你的代码是否会达到return语句?

在另一种情况下,如果您的功能确实返回,那么将会调用fileOut.close();吗?

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

Im not sure on the proper way to divide up my existing code to create a new method.

这真的取决于你和什么样的代码是干什么的,但有几个原则:

  • 方法过于漫长理解?分解成几种方法。
  • 你在写“重复的代码”吗?也许这应该是一种方法。
  • 像写入文件的东西是一个独立的操作单元。换句话说,与程序其余部分的逻辑分开。所以应该把它作为自己的方法分开。
  • 等等
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 
    } 
0

其他人指出了几件事。

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