2014-01-10 2 views
0

10 개의 부동 소수점 숫자를 입력으로 사용하는 프로그램을 작성하고 평균보다 큰 모든 숫자 뒤에 숫자의 평균을 표시합니다. 매개 변수로 double 배열을 사용하고 배열의 데이터 평균을 반환하는 메서드를 사용하고 있습니다. 그러나 내 문제는 내 프로그램을 실행할 때 출력 창이 완전히 비어 있다는 것입니다. 나는 이것이 내 방법을 주된 방법으로 부르지 않았기 때문이라고 생각한다. 그러나, 그 코드를 작성하는 방법을 모르겠습니다. 고맙습니다.출력 공백 - 배열의 평균을 계산하는 Java 프로그램

import java.util.Scanner; 

public class Average { 

public static void main(String[] args) { 
} 

public double average(double[] number) { 
    Scanner scanner = new Scanner(System.in); 

    int x = 0; 
    double sum = 0; 
    double[] numberList = new double[10]; //array to hold all numbers 
    double[] largerList = new double[10]; //array to hold numbers greater than the average 


    int numberIndex = 0; 
    int largerIndex = 0; 



    System.out.printf("Please enter 10 floating-point numberes.\nIf more than 10 values are entered, the numbers following 10 are ignored.\nIf less than 10 numbers are entered, the program will wait for you to enter 10.\n"); 

    for (int i = 0; i < 10; i++) { 


     try { //try catch exception to catch decimal inputs as well as more /less than 10 integers 
      x = scanner.nextInt(); 
      sum += numberList[x]; //add up all inputs to find sum 
     } catch (Exception e) { 
      System.out.println("Invalid input! Please reenter 10 integer values."); 
      scanner = new Scanner(System.in); 
      i = -1; 
      numberIndex = 0; 
      largerIndex = 0; 
      numberList = new double[10]; 
      largerList = new double[10]; 
      continue; 
     } 
    } 

    for (int i = 0; i < number.length; i++) { 
     sum = sum + number[i]; 
     double average = sum/number.length; 
     System.out.println("Average value of your input is: " + average); 
     System.out.println(); 

     //return average; 

     if (x > average) { 
      largerList[largerIndex] = x; //add negative input to negativeList array 
      largerIndex = largerIndex + 1; 

     } 
    } 



    for (int i = 0; i < largerIndex; i++) { 
     System.out.println(largerList[i]); 
    } 
    return 0; 





} 

}

+0

공공 정적 무효 메인 (문자열 []에 args) { \t 새로운 평균() 평균 (새로운 더블 [10]).; } – MrSimpleMind

답변

2

주요 질문에 대답 할 수 ...

그러나, 내 문제는 내가 내 프로그램을 실행할 때 출력 창을 완전히 비어 있다는 것입니다. 나는 이것이 내 방법을 주된 방법으로 부르지 않았기 때문이라고 생각한다. 그러나, 그 코드를 작성하는 방법을 모르겠습니다.

public static void main(String[] args) { 
    new Average().average(new double[10]); 
} 

또는 같은 어쩌면 당신이 생각하는 뭔가 ...

public static void main(String[] args) { 
    double[] numbers = {2,3,4,5,6,4,3,2,1,3}; 
    new Average().average(numbers); 
} 

(주어진 복식) 위의 A 출력 실행 : 당신이있는 경우

Please enter 10 floating-point numberes. 
If more than 10 values are entered, the numbers following 10 are ignored. 
If less than 10 numbers are entered, the program will wait for you to enter 10. 
2 
3 
3 
4 
1 
2 
3 
4 
5 
1 
Average value of your input is: 0.2 

Average value of your input is: 0.5 

Average value of your input is: 0.9 

Average value of your input is: 1.4 

Average value of your input is: 2.0 

Average value of your input is: 2.4 

Average value of your input is: 2.7 

Average value of your input is: 2.9 

Average value of your input is: 3.0 

Average value of your input is: 3.3 

1.0 
1.0 
1.0 
Press any key to continue . . . 

코드 자체에 대해 궁금한 점이 있다면 새로운 질문을 작성하거나 더 명확하게 편집하는 것이 좋습니다.

코딩을 잘 해보십시오.

1

귀하의 방법 average()은 두 배의 배열을 취한 다음 표준 입력에서 다른 배열을 검색합니다. 그건 말이되지 않습니다.

두 개를 가져 와서 메서드에 전달하거나 메서드에 전달하지 않고 표준 입력에서 가져옵니다.

0

main 방법으로 Average 클래스의 인스턴스를 만들고 average() 메서드를 호출하면됩니다.

사용자가 입력을 받아 들일 때 메서드에서 double 배열을 구문 분석하는 이유는 무엇입니까?

0
well,you are using a non static method "average()" for your task which needs an instances of the class to run which are not creating anywhere.so there are only two options:- 

#1.create an instances of your class then call it. 
    public static void main(String... s) 
    { 
     Average obj=new Average(); 
     obj.average(); 
    } 


#2.make "average()" a static method by adding static keyword. 

    public static double average() 
    { 

    //your code..... 
    } 

    public sttatic void main(String... s) 
    { 

     average(); 
    } 
your dont need to keep an argument in your method. 
관련 문제