2016-08-20 3 views
1

현재 Java 프로그래밍 클래스의 과제를 진행하고 있습니다. 나는 나 자신을 조금 묶어 놓은 것처럼 보인다. 내가 뭘 잘못하고 있는지 깨닫도록 돕는 데 도움이된다면 크게 감사 할 것입니다.학교 과제 다차원 배열 문제

할당

는 다음을 수행하는 프로그램 작성 :

다중 차원 배열

에 아래의 데이터를 넣어은 직원의 급여 통계를하고자하는 회사에 대한하라는 메시지를 표시합니다.

평균 직원 급여를 이중으로 반환하는 방법을 작성하십시오. 회사 번호와 직원 임금을이 방법에 전달하십시오.

총 직원 급여를 int로 반환하는 메소드를 작성하십시오. 회사 번호와 직원 임금을이 방법에 전달하십시오.

직원 수를 int로 반환하는 메서드를 작성합니다. 회사 번호와 직원 임금을이 방법에 전달하십시오.

기본 메소드에서 다른 메소드를 호출하고 결과를 출력하십시오.

저는 여전히 새롭고 프로그래밍 원칙을 이해하는 데 어려움을 겪습니다.

내가 대신 방법 계산의 위치를 ​​얻고 프로그램 (나쁜 출력)를 실행하면 :

bad output

여기

내가 지금까지 무엇을 가지고 :

package salaries; 

import java.util.Scanner; 

public class Salaries { 

    /** 
    * @param args the command line arguments 
    */ 
    public static void main(String[] args) { 

     //declare, instantiate, and define value of multi array [3] [12] 
     double [][] mSalary = { { 49920, 50831, 39430, 54697, 41751, 36110, 
           41928, 48460, 39714, 49271, 51713, 38903}, 
          { 45519, 47373, 36824, 51229, 36966, 40332, 
           53294, 44907, 36050, 51574, 39758, 53847}, 
          { 54619, 48339, 44260, 44390, 39732, 44073, 
           53308, 35459, 52448, 38364, 39990, 47373}}; 

     //declare, instantiate, and define value 
     //of single array for company names 
     //and output values to user for selection 
     String [] company = { "Alhermit", "Logway", "Felter" }; 
     for(int i = 0; i < company.length; i++) 
      System.out.println("Company " + i + " : " +company[i]); 

     Scanner scan = new Scanner(System.in); 
     int cCompany; 
     do{ 
      //ouput for user to select a company 
      System.out.print("Select company: (0)" +company[0]+ ", (1)" 
          +company[1]+ "; (2)" +company[2]+ " > "); 
      //scan user input into cCompany 
      cCompany = scan.nextInt(); 


      //call number method 
      num nums = new num(); 
      nums.number(mSalary, cCompany); 

      //call total method 
      total sum = new total(); 
      sum.total(mSalary, cCompany); 

      //call average method 
      avg cAvg = new avg(); 
      cAvg.average(mSalary, cCompany); 


      //output statistics to user on selected company 
      System.out.println("You have selected the company " + company[cCompany] + ". "); 
      System.out.println(company[cCompany] + " has " + nums + " of employees."); 
      System.out.println("A total employee salary of " + sum + "."); 
      System.out.println("The average employee salary is " + cAvg); 
     } 
      while(cCompany < 0 || cCompany > 2); 
    } 
} 

//total class to calculate 
//salary of user selected company 
class total { 

    public static int total(double [][] mSalary, int cCompany){ 

     //assign variables 
     int sum = 0; 

     //for loop to calculate salary total of user input company 
     for(int j = 0; j < mSalary[cCompany].length; j++){ 
      sum += mSalary[cCompany][j]; 

     } 

    //return statement 
    return sum; 
    } 
} 

//average class to calculate 
//average of user selected company 
class avg { 

    public static double average(double [][] mSalary, int cCompany){ 

     //assign variables 
     int cAvg = 0; 
     int sum = 0; 
     int count = 0; 

     //totals the values for the selected company by 
     //iterating through the array with count. 
     while(count < mSalary[cCompany].length){ 
      sum += mSalary[cCompany][count]; 
      count +=1; 
     } 

      cAvg = sum/mSalary[cCompany].length; 
      return cAvg; 
    } 
} 
//number class to calculate amount of 
//employees in user selected company 
class num { 

    public static int number(double [][] mSalary, int cCompany){ 

     //assign variables 
     int nums = 0; 

     //number of employees based on length of colomn 
     nums = mSalary[cCompany].length; 
     return nums; 
    } 
} 
+0

"나쁜 출력"이미지에 링크합니다 (이미지를 사용하지 말고 질문에 텍스트로 포함). 그러나 "올바른 출력"이 무엇인지는 말하지 않았습니다. 당신이 현재 붙잡고있는 과제의 어느 단계도 아닙니다. –

+0

잘못된 클래스 이름, 잘못된 들여 쓰기, 일치하지 않는 공백을 포함하여 많은 스타일 문제가 있습니다. –

답변

1

nums, sum, cAvg은 모두 가지고있는 클래스의 인스턴스이므로 해당 클래스의 인스턴스를 인쇄하고 있습니다.

은 (그런데 - 당신은이 클래스의 이름을 변경해야 클래스는 대문자로 시작 그것은 변수에서 그들을 구별합니다...)

이 문제 두 가지가 있습니다.

  1. 당신은 데이터가없는를 포함하고 더 toString 방법이없는 클래스를 인스턴스화합니다.
  2. 데이터를 반환하는 정적 메서드 만있는 클래스를 인스턴스화합니다. 클래스를 인스턴스화 할 필요가 없습니다. 모두; 대신 메소드 호출의 결과를 인쇄하십시오. 같은 이러한 호출 중 하나 이상을 바꿀 것

:

System.out.println(company[cCompany] + " has " + num.number(mSalary, cCompany); + " of employees."); 

나는 독자들에게 운동으로 나머지를 둡니다.

+0

답장을 보내 주셔서 감사합니다. 정확하게해야 할 곳이 있습니다. 정적 메서드를 인스턴스화하기 위해 불필요한 코드를 제거하고 출력 결과를 바꿔서 반영했습니다. 또한 클래스 이름으로 대문자로 문제를 해결했습니다. 모든 것이 훌륭하게 작동합니다. 답장이 빠르며 엄청난 도움이되었습니다. 고맙습니다!! – Cezpool

+0

작업을 마친 후 다시 돌아가서 세 클래스의 선언을 제거하고 주 클래스 내부에 메서드 코드를 배치 한 다음 변경 내용을 반영하도록 출력 문을 변경했습니다. 코드가 적고 처리량이 적으며 훨씬 깨끗하게 보입니다. – Cezpool