2013-09-23 3 views
0

내 프로그램을 메소드 (특히 모든 메소드를 포함하는 메인 메소드와 다른 메소드)로 나눌 때 문제가 있습니다. 새로운 방법을 만들기 위해 기존 코드를 분할하는 적절한 방법은 확실하지 않습니다. 내 프로그램도 파일에 씁니다. 내가 코드를 컴파일 할 때자바 프로그램에서 메소드 사용하기

은 내가

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

말하는 오류가 발생하지만 난 이미 return 문이있다.

제대로 사용 했습니까? 또는 무엇이 잘못 되었습니까? 감사 NO 방법

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 

SAME USING CODE BUT 방법

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();을 호출 할 수 있습니까?

자바는 return 문에 도달하지 못할 수도 있고,없는 것처럼 행동합니다. 이 문제를 해결하려면 함수의 끝에 루프가 입력되지 않은 가장자리 케이스를 처리하는 "기본"return 문이 있어야합니다.

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

정말 당신에게 달려과 코드가 무엇을하고 있는지,하지만 몇 가지 가이드 라인 :

  • 방법을 이해하는 데 시간이 너무 오래 점점? 몇 가지 방법으로 나누십시오.
  • "중복 코드"를 작성 하시겠습니까? 어쩌면 그것은 방법으로 가야 할지도 모릅니다.
  • 파일 쓰기와 같은 작업은 개별적인 작업 단위입니다. 즉, 나머지 프로그램 논리와 분리하십시오. 따라서 자체 메서드로 분리해야합니다.
  • 등은
0

있어서 잘못 WITH

ORIGINAL CODE. 반환 값을 Double로 선언했지만 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이라고 생각합니다. 특정 경우에 한 곳에서 무언가를 계산하고 데이터를 파일에 보존하는 것은 두 가지 명확하고 명확한 관심사입니다.

관련 문제