시험에 질문이 있습니다. 처음에는 꽤 쉬웠습니다. 나에게 질문을 설명하자단일 메서드에서 여러 값 반환
은 사용자가 (배열)를 넣어 원하는 얼마나 많은 정수를 지정 확인을
- 이 (까다로운) 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]);
}
}
어떤 문제가 발생 했습니까? –
말 : 모든 숫자가 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 –