2013-09-23 30 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。在你的特定情況下,在一個地方計算某些東西,並將數據保存到一個文件中是兩個截然不同的問題。