2013-12-11 3 views
0

Write the following methods:
13. A method named loadScores that will be passed the size of an array, prompt the user to enter the appropriate number of floating point grades, then return the loaded array.인수를 사용하여 메서드 호출

메서드를 작성했지만 호출하려고 할 때 문제가 발생합니다.

public class ArrayHandout { 
    public static void main(String[] args) { 
    int size = loadScores(size); 
    double[] scoresArray = loadScores(size); 
    } // end main 

    public static double[] loadScores(int size) { 
    Scanner input= new Scanner(System.in); 

    System.out.println("How many scores would you like to enter?"); 
    size = input.nextInt(); 
    double[] scoresArray = new double[size]; 

    int i; 
    for(i = 0; i < scoresArray.length; i++) { 
     System.out.println("Please enter a score:"); 
     scoresArray[i] = input.nextDouble(); 
    }// end for 

    System.out.println(scoresArray[i]); 
    return scoresArray; 
    } // end loadScores 
} // end class 

이 내가 원래의 코드를 쓸 때 내가 가진 한 일부 오류를 수정하려는 시도에 몇 가지 변경, 불행하게도, 심지어 이러한 변경 사항이 무엇인지 기억하거나 고정 된 경우하지 않습니다 여기에 내가 가진 무엇 문제는 내가 지금 컴파일 할 수 없기 때문이다. 컴파일 할 때 얻을 수있는 문제는 전체 배열을 인쇄하는 것이 아니라 배열의 첫 번째 번호 만 인쇄한다는 것입니다.

1 error found: File: C:\Users\HiTechRedneck\Desktop\Fall 2013\PlayingwithJava\ArrayHandout.java [line: 6] Error: incompatible types required: int found: double[]

내가 이전에 내가 때문에 size 요구 main에 선언 할 것을 알고 : 내가 말했듯이 그 문제는 지금부터 수정 된 경우 나는 다음과 같은 오류 메시지가 나타납니다 컴파일 할 때, 나도 몰라 점점 "변수 크기를 찾을 수 없습니다"오류가 발생했습니다.하지만 사실은 내가 선언하고있는 변수이기 때문에 loadScores(size)int size으로 선언하려고 시도하고 있다는 것을 알았습니다. 선언하는 변수도 메소드의 인수입니다.

도움을 주시면 감사하겠습니다.

+2

'int size = loadScores (size);' 그것은 당신이 원하는 것이 아닙니다. 'size'는 5와 같이 숫자 여야하지 않습니까? – kevinsa5

+0

하지만 크기는 사용자가 메서드에서 입력 한 내용에 따라 달라 지므로 그만큼 막혀 있습니다. –

+0

함수 안에서'size'를 읽는다면 함수의 매개 변수로 가져올 필요가 없습니다 - 그것은 읽히기 전에'size' 값을 알고 있다는 것을 의미합니다! – kevinsa5

답변

0

고객님의 전화 번호는 int size = loadScores(size)입니다. loadScores의 반환 유형은 double[]이므로 반환 값을 이와 같이 지정해야합니다.

그냥 한 줄을 삭제하면 모든 것이 예상대로 작동합니다.

0

int size = loadScores(size);은 loadScores가 double 유형의 배열을 반환하기 때문에 오류가 발생합니다.

작업이 단지 코드가 아마 크기를 임의의 값을주고 그에서 통과 아니면 주에서 크기를 제거하고 그냥 숫자 통과 얻을 수 있었다 니펫을 원하는 이후 :.

public static void main(String[] args) { 
    double[] scoresArray = loadScores(5); 
} 

편집 : 또한 for 루프 이후의 print 문은 배열의 길이보다 커서 ArrayIndexOutOfBoundsException을 던질 것입니다. scoresArray의 내용을 인쇄하려면 각 요소를 통과하는 다른 루프가 필요합니다.

두 번째 편집 : 사용자에게 크기를 묻는 메시지가 표시되면 main 메소드에서 해당 메시지를 실행 한 다음 loadScores()로 전달해야합니다.

public static double[] loadScores() 

을 메인에

loadScores() 

전화 :

0

당신은 당신이 기능 에서 그것을 읽을 함수를 호출하기 전에 선언이 될 수 모든 크기를 필요가 없습니다.예 :

public class ArrayHandout { 
    public static void main(String[] args) { 
    loadScores(); 
    } 

    public static void loadScores() { 
    Scanner input= new Scanner(System.in); 

    System.out.println("How many scores would you like to enter?"); 
    int size = input.nextInt(); 
    double[] scoresArray = new double[size]; 

    int i; 
    for(i = 0; i < scoresArray.length; i++) { 
     System.out.println("Please enter a score:"); 
     scoresArray[i] = input.nextDouble(); 
    }// end for 

    System.out.println(scoresArray[i]); 
    } 
} 

방법이 아무것도 반환하지 않는 경우 (당신이하여 지정해야합니다 :

public class ArrayHandout { 
    public static void main(String[] args) { 
    double[] scoresArray = loadScores(); 
    //do whatever you want here or change declaration of 
    //the function to void and do not return anything 
    } 

    public static double[] loadScores() { 
    Scanner input= new Scanner(System.in); 

    System.out.println("How many scores would you like to enter?"); 
    int size = input.nextInt(); 
    double[] scoresArray = new double[size]; 

    int i; 
    for(i = 0; i < scoresArray.length; i++) { 
     System.out.println("Please enter a score:"); 
     scoresArray[i] = input.nextDouble(); 
    }// end for 

    System.out.println(scoresArray[i]); 
    return scoresArray;  
    } 
} 

당신은 이런 식으로 그것을 할 더 좋을 것이다 반환 된 배열을 사용하지 않는 경우 서명에 void 키워드 사용) - 유효성 검사가 필요하지만 특정 문제에만 응답했습니다. 유효성 검사없이 호환되지 않는 값이 제공되면 프로그램이 계속 실패합니다.

관련 문제