2017-03-04 5 views
-1

시험에 질문이 있습니다. 처음에는 꽤 쉬웠습니다. 나에게 질문을 설명하자단일 메서드에서 여러 값 반환

  • 은 사용자가 (배열)를 넣어 원하는 얼마나 많은 정수를 지정 확인을

  • 이 (까다로운) 1500 -1500 사이
  • 숫자는해야한다 : 방법을 만들기 놓은 숫자의 백분율을 계산합니다. 백분율은 0, 양수, 음수입니다.

내가 제대로했다고 생각하지만, (이 모든 것은, 반환 결과는 double 값을해야 ONE 방법, 문자열로 돌아올 수, 문자열 버퍼에 있어야) 나는 실시간으로 테스트 할 때 , 내가 실행하는 문제는 0.0, 0.0, 0.0 값을 반환한다는 것입니다. 또는 값이 동일한 조건 (예 : 모두 0)에 속하는 경우에만 올바른 백분율을 반환합니다.

내 코드는 다음과 같습니다. (귀하가 이해할 수 있기를 바랍니다.) 나는 단지 궁금해서, 어떻게 해결해야할지 모르겠다. 나는 또한 정적 메서드로 시도했다, 나는 같은 문제가 발생합니다.

import java.util.Scanner; 

public class nPNZ { 
    public int [] number; 

    public nPNZ (int [] n){ 
     number = n; 
    } 

    public double [] allCalcs(){ 
     int countPos = 0; int countNeg = 0; int countZero = 0; 
     for (int i = 0; i < number.length; i++) { 
      if (number[i] == 0) { 
       countZero++; 
      }else if (number[i] > 0){ 
       countPos++; 
      }else{ 
       countNeg++; 
      } 
     } 
     //0 = 0 ; 1 = positive ; 2 = negative 
     double total [] = new double [3]; 
     total[0] = (countZero/number.length)*100; 
     total[1] = (countPos/number.length)*100; 
     total[2] = (countNeg/number.length)*100; 
     return total; 
    } 

    public static void main (String args[]){ 
     //min 20 number, -1500 to 1500 
     Scanner input = new Scanner (System.in); 

     final int MIN_SIZE = 5; 
     int size = 0; 

     while(size < MIN_SIZE){ 
      System.out.println("Specify the size of the array: "); 
      size = input.nextInt(); 
      while(size<MIN_SIZE){ 
       System.out.println("Size should be greater than: " + MIN_SIZE); 
       size = input.nextInt(); 
      } 
     } 

     input.nextLine(); 

     int num [] = new int [size]; 

     for (int i = 0; i < num.length; i++) { 
      System.out.println("Write number " + (i+1) + " : "); 
      num[i] = input.nextInt(); 
      while(num[i] < -1500 || num[i] > 1500) { 
       System.out.println("The number should within the range of -1500  and 1500"); 
       num[i] = input.nextInt(); 
      } 
     } 

     nPNZ n = new nPNZ (num); 
     System.out.println("Percentage of zero numbers is: " + n.allCalcs()[0]); 
     System.out.println("Percentage of positive numbers is: " + n.allCalcs()[1]); 
     System.out.println("Percentage of negative numbers is: " + n.allCalcs()[2]); 
    } 
} 
+1

어떤 문제가 발생 했습니까? –

+0

말 : 모든 숫자가 0, 0, 0, 0, 0 인 경우 5 개의 숫자를 넣습니다. 출력에서 ​​올바른 질문을 얻습니다. 숫자의 퍼크는 100.0 포지티브 누벨의 퍼즈 이다 음수 0.0 흡수율은 0, 0, 0, 3, -1 출력은 다음과 같다 : 제로 번호 퍼크이다 긍정적 인 수의 0.0 Perz이다 그러나 I라고 놓으면 0.0 : 0.0 음수의 퍼크 : 0.0 –

답변

1

코드에 하나의 문제점이 있습니다. 여기

double total [] = new double [3]; 
total[0] = (countZero/number.length)*100; 
total[1] = (countPos/number.length)*100; 
total[2] = (countNeg/number.length)*100; 
return total; 

, countZero, countPoscountNeg 모든 정수 및 number.length도 정수입니다. 따라서 정수를 다른 정수로 나눌 때 정수를 얻을 수 있습니다.

countZero, countPoscountNegnumber.length 이상의 모든 적은이다, 때문에, 당신은 total 배열의 제로 값을 얻을 것이다.

예 :

System.out.println(2/3); // prints 0 
System.out.println(2.0/3); // prints 0.6666666666666666 
System.out.println((double)2/3); // prints 0.6666666666666666 

이 문제를 해결하기 위해 몇 가지 대안이있다. 그것들 중 하나는 단순히 다른 정수로 나눈 값을 1.0으로 곱하는 것입니다.

다음과 같이 할 수 있습니다.

public double [] allCalcs(){ 
    // your code goes here 
    double total [] = new double [3]; 
    total[0] = (countZero * 1.0/number.length) * 100; 
    total[1] = (countPos * 1.0/number.length) * 100; 
    total[2] = (countNeg * 1.0/number.length) * 100; 
    return total; 
} 

OR, 당신은 이중으로 변수 (countZero, countPoscountNeg)를 선언 할 수 있습니다.

+0

아, 고마워요.내가 만들었 : double countPos = 0; double countNeg = 0; double countZero = 0; 대단히 감사합니다. 이제 문제는 해결되었습니다. –