2013-10-24 2 views
0

나는 을 BlueJ에 사용하고 있으며 나는 그것에 대해 상당히 익숙하다. Java에서 너무 좋지 않은 것이 있다면 배열입니다. 2 차원 배열은 말할 것도 없습니다. 희망을 갖고 누군가가이 프로그램에서 나를 도울 수 있습니다. 다소 압도되어서 어디서부터 시작해야할지 모르겠습니다.2 차원 배열 문제. 도움 요청

이 프로그램의 목적은 2 차원을 사용하여 .TXT 파일의 정치적 투표 결과를 집계하는 것입니다. 입력 파일 "PROG4IN.TXT"에는 폴링 된 각 유권자에 대한 한 줄이 들어 있습니다. 각 행은 선호하는 후보자의 이름과 유권자의 나이를 포함합니다. 2 행 3 열의 배열을 사용하여 유권자를 선호 후보자와 연령 그룹별로 집계해야합니다. 3 세 그룹은 18-29, 30-49, 50-99입니다.

가 원하는 최종 출력처럼 보이도록되어있는 것이다 :

Krook 45 
Leyer 40 
Krook 76 
Leyer 55 
Krook 20 
Krook 50 
Leyer 28 
Krook 30 
Leyer 23 
Krook 72 
Krook 42 
Krook 81 
Leyer 64 
Krook 52 
Leyer 18 
Leyer 34 
Krook 60 
Krook 26 
Leyer 49 
Krook 37 

내가 사용해야합니다 참고로

Candidate 18-29 30-49 50-99 Total 
Krook   2  4  6  12 
Leyer   3  3  2  8 

, 이것은 "PROG4IN.TXT"파일에 무엇인가 이 템플릿 :

public class Table { 
    private int[][] table; 
    private String[] names; 

    public Table() { 
     // Create the two-dimensional tally array "table" 
     // having 2 rows and 3 columns. Row 0 corresponds 
     // to candidate Krook and row 1 to candidate Leyer. 
     // The columns correspond to the three age groups 
     // 18-29, 30-49, and 50-99. Initialize all the 
     // tallies to zero. Create the array "names" to 
     // hold the candidate names: names[0]="Krook" and 
     // names[1]="Leyer". 
    } 

    public void tally(String name, int age) { 
     // Add one to the tally in the "table" array that 
     // corresponds to the name and age passed as arguments. 
     // Hint: Use the equals method to determine whether 
     // two strings are equal: name.equals("Krook") is 
     // true when name is "Krook". 
    } 

    public void report() { 
     // Use nested loops to print a report in the format 
     // shown above. Assume that the tallies have already 
     // been made. 
    } 
} 

결국, 나는 그 테이블 개체를 생성하는 메인 클래스를 만들어야 만한다. PROG4IN.TXT에서 데이터를 가져 와서 보고서를 인쇄합니다.

누군가 나를 도와 줄 수 있기를 바랍니다.

미리 감사드립니다.

답변

0

귀하의 배열은 다음과 같이 표시됩니다

table = new int[2][3](); 

를하고 다음과 같이 될 것입니다 내부 :

{ 
{count for 1st age group for krook, for 2nd age group for krook, last group for krook} 
{count for 1st age group for leyer, for 2nd age group for leyer, last group for leyer} 
} 

그래서 당신은 예를 들어, leyer의 중간 연령 그룹에 추가 할 때, 당신은 테이블 [1] [1] + = 어떤 금액을 할 것입니다.

또는 krook의 세 번째 연령 그룹 인 youd do table [0] [2] + = someamount.