2015-01-24 2 views
0

이 코드와 관련하여 질문이 있습니다. 일부 메소드에서는 리턴과 주석이 있음을 알 수 있습니다. 그 이유는 void 메소드 대신 return 메소드를 사용해야하기 때문입니다. 선생님은 저를 무효 클래스로 변환하라고 말씀 하셨지만, 반환 할 필드 변수를 수정하는 방법이 아닙니까? 때로는 선생님이 프로그래밍에 대해 많이 알지 못하는 것 같거나 의심이 있기 때문에 의심의 여지가 있습니다. 사전에 도움을 주셔서 감사드립니다.자바의 메소드 선언

공용 클래스 ArraysClass {

private int[] array; 
private int arrayLength; 

public ArraysClass() { 
    setArrayLength(); 
    array = new int[arrayLength]; 
} 

public int setArrayLength() { 
    Scanner scanner = new Scanner(System.in); 
    System.out.println("Enter a number to set the length of the array:"); 
    arrayLength = scanner.nextInt(); 
    System.out.println(); 
    return arrayLength; 

} 

public void fillArray() { 
    Scanner scanner = new Scanner(System.in); 

    for (int i = 0; i < arrayLength; i++) { 
     System.out.println("Type a number to fill position " + i); 
     array[i] = scanner.nextInt(); 
    } 
    // return array; 
    System.out.println(); 
} 

public void findNumber() { 
    Scanner scanner = new Scanner(System.in); 

    int tofind, position; 
    System.out.println("Enter a number to search it in the array:"); 
    tofind = scanner.nextInt(); 
    position = Arrays.binarySearch(array, tofind); 
    if (position < 0) { 
     System.out.println("We did not find your number."); 
    } else { 
     System.out.println("The number you typed is in the next position: " + position); 
    } 
    System.out.println(); 
} 

public void fillMethod() { 
    Scanner scanner = new Scanner(System.in); 

    int tofill; 
    System.out.println("Enter a number to fill the entire array with:"); 
    tofill = scanner.nextInt(); 
    Arrays.fill(array, tofill); 
    System.out.println(); 
    //return array; 
} 

public void Sortmethod() { 
    Arrays.sort(array); 
    //return array; 
} 

private void showArray() { 
    System.out.println("Showing the array..."); 
    for (int i = 0; i < arrayLength; i++) { 
     System.out.println(array[i]); 
    } 
    System.out.println(); 
} 

public static void main(String[] args) { 
    Scanner scanner = new Scanner(System.in); 
    ArraysClass arrayobj = new ArraysClass(); 
    int choose; 
    do { 
     do { 
      System.out.println("1-Fill the array"); 
      System.out.println("2-Find a number in the array"); 
      System.out.println("3-Fill the entire array with a number"); 
      System.out.println("4-Sort the array"); 
      System.out.println("5-Show the array"); 
      System.out.println("6-Exit"); 
      System.out.println("Which one do you want to use?:"); 
      choose = scanner.nextInt(); 
     } while (choose < 1 && choose > 6); 
     switch (choose) { 
      case 1: 
       arrayobj.fillArray(); 
       break; 
      case 2: 
       arrayobj.findNumber(); 
       break; 
      case 3: 
       arrayobj.fillMethod(); 
       break; 
      case 4: 
       arrayobj.Sortmethod(); 
       break; 
      case 5: 
       arrayobj.showArray(); 
       break; 
      case 6: 
       break; 
     } 

    } while (choose != 6); 

} 

} 일반적으로

답변

0

는 방법은 return 뭔가 당신은 그것에서 값을 필요로하는 경우는. 일부 프로그래머는 성공 또는 실패를위한 유일한 방법 인 경우에도 returnboolean 또는 상태 코드의 경우 int을 사용합니다. 나는 이러한 접근법을 따르지 않는다. 메소드를 구현할 때, 나는 항상 어떻게 그 메소드를 사용하고 싶은지 스스로에게 묻는다. 그것으로부터 가치가 필요하다면 그 유형을 가질 것입니다. 그렇지 않으면 void이됩니다. 우리가 당신의 방법을 보자 :

setArrayLength : 일반적으로,이 이름에서 내가 void을 할 길이와 방법을 나타내는, 당신이 그것을에 int를 통과 할 것으로 기대한다. 이것은 setter에게는 매우 일반적이지만 여기서는 int 매개 변수를 사용하는 것보다 분명히 열등한 메서드 내에서 실제 값을 읽는 것입니다. 배열 length에서 읽지 않은 값을 사용하여 배열을 설정하려는 경우 메서드가 쓸모 없게됩니다. 콘솔.

fillArray :이 값은 void 일 것으로 예상됩니다. 따라서이 선언에 동의하지만 다시 읽는 부분이 없어야합니다.

findNumber는 : 수를 매개 변수로 찾을 수 얻을 발견되지 않는 경우는 인덱스, -1을 나타내는 int를 반환해야합니다.

fillMethod : 배열을 채우는 데 사용할 값을 나타내는 int 매개 변수가 있어야하며 무효 여야합니다.

sortMethod : ok, 아마도 return 결과 배열이지만 필요에 따라 다릅니다.

showArray : 나는 System.out에로 출력 필요하지 않습니다,

일반 실수를 거기 PrintStream 기대 : 당신은 콘솔 인/아웃 작업에와 방법을 혼합, 코드는이 충분하지 일반적이다 방법.

+0

죄송 합니다만, 입/출력 작업과 메서드를 혼합하면 무엇을 의미합니까? 나는 그것을 정말로 이해하지 못합니다 – Seveeen

+0

예를 들어 fillArray에서 : Scanner scanner = new Scanner (System.in); System.in에서 항상 가치를 읽을 수 있다는 것을 어떻게 미리 알 수 있습니까? array.length라고 부르려고합니다. 당신은 그렇게 할 수 없습니다. 코드에서는 항상 콘솔에서 값을 읽고 콘솔에 값을 쓰는 것으로 가정합니다. –