2011-10-03 2 views
0

이것은이 웹 사이트의 첫 번째 게시물이므로 인내심을 바랍니다. 저는 Java를 연구 중이며 두 개의 배열로 된 저장소를 만들려고합니다. 선수의 이름과 출석. '사용자 인터페이스'에 JOptionPane을 사용하고 있습니다. 사용자가 요청할 때 이름과 각자의 출석을 표시하고 싶습니다. 는 여기가 (이 완료되지 않을 것) 내 코드입니다 :배열에서 Java의 모든 내용을 표시하는 방법

import javax.swing.*; 
import java.text.*; 

public class Pelada{ 
    public static void main(String []args){ 
     String[] players = new String[10]; 
    int[] attendance = new int[10]; 
    int x = 0, z = 0, control = 0, posPlayer = 0; 
    String test; 

    while(control != 4){ 
     control = Integer.parseInt(JOptionPane.showInputDialog(null,"1- Add  new players \n 2- List \n 3- Increment attendance \n 4- Delete player \n 4- Exit", "Choose an option below", JOptionPane.INFORMATION_MESSAGE)); 
      if(control == 1){ 
       players[x] = JOptionPane.showInputDialog(null, "New player: ", "Add New Player", JOptionPane.INFORMATION_MESSAGE); 
       attendance[x] = Integer.parseInt(JOptionPane.showInputDialog(null, "How many matchs have he played so far? ", "Attendance", JOptionPane.INFORMATION_MESSAGE)); 
       x++; 
      } 
      else if(control == 2) 
        for (int i=0; i < players.length; i++){ 
        JOptionPane.showMessageDialog(null, "Attendance = " + attendance[i], "N: " + i + "- " + players[i], JOptionPane.WARNING_MESSAGE);  
        } 
       else if(control == 3){ 
         posPlayer = Integer.parseInt(JOptionPane.showInputDialog(null, "Choose the player id: ", "Player Id", JOptionPane.INFORMATION_MESSAGE)); 
         attendance[posPlayer] = Integer.parseInt(JOptionPane.showInputDialog(null, "Increment ", "Attendance", JOptionPane.INFORMATION_MESSAGE)); 
        }     
    } 

} 

} 대신에 두 개의 배열을 갖는

+0

귀하의 궁금한 점은 무엇입니까? 코드에 이미 모든 플레이어가 출석과 함께 나열되어 있습니다. 그렇지 않습니까? – Howard

+0

최근 버전의 Java를 사용하고 있다면, 배열을 수동으로 반복하고 각 요소를 가져 오는 대신 for-each 루프 구조 (for (type x : listOfXs) {...}'를 살펴보십시오. @ dogbane의 제안에 따라 좀 더 OO-correct가되면 사소한 일이 될 것이고 상용구 코드로 인한 혼란을 크게 줄일 수있을 것입니다. –

+0

또한, 그냥 제안하지만, 들여 쓰기 (그리고 아마도 whitespacing뿐만 아니라 수정). 당신이 일관되게 들여 쓰기한다면 (우연히 들여 쓰는 정도에 관계없이) 우연히 그 대신에 들여 쓰기하면 코드를 훨씬 쉽게 읽을 수 있습니다. –

답변

1

;

public class Player { 
    private final String name; 
    private final int attendance; 

    public Player(String name, int attendance) { 
     this.name = name; 
     this.attendance = attendance; 
    } 

    public String getName() { 
     return name; 
    } 

    public int getAttendance() { 
     return attendance; 
    } 
} 

그런 다음 Player 객체를 생성하고이 ArrayList에 저장 : 플레이어 하나 출석 한, 당신의 코드가 더 객체 지향 선수의 이름과 참석을 캡슐화 Player 클래스를 만들어합니다. 얼마나 많은 플레이어가 추가 될지 알지 못한다면 배열을 사용하지 마십시오.

List<Player> players = new ArrayList<Player>(); 

if (control == 1) { 
    String name = JOptionPane.showInputDialog(null, "New player: ", "Add New Player", 
      JOptionPane.INFORMATION_MESSAGE); 
    int attendance = Integer.parseInt(JOptionPane.showInputDialog(null, 
      "How many matchs have he played so far? ", "Attendance", JOptionPane.INFORMATION_MESSAGE)); 
    Player player = new Player(name, attendance); 
    players.add(player); 

} else if (control == 2) { 
    for (int i = 0; i < players.size(); i++) { 
     Player player = players.get(i); 
     JOptionPane.showMessageDialog(null, "Attendance = " + player.getAttendance(), "N: " + i + "- " + player.getName(), 
       JOptionPane.WARNING_MESSAGE); 
    }     
} 
+0

이것은 훌륭한 해결책이었습니다. 내가 초심자 인 것에 따라 이해하는 것은 나를 데려 갔다. 작성된 코드에 관한 한 가지 의심 Player Player = player.get (i); Player 클래스에서 다른 객체를 만드는 중입니까? 내가 옳다면 왜 새로운 것을 사용해야합니까? 고마워요 – Camus

+0

'Player player = players.get (i);''선수 'arraylist에서 색인'i'에있는 플레이어를 반환합니다. 새 오브젝트를 작성하지는 않지만 목록에서 기존 오브젝트를 검색합니다. – dogbane

+0

그래서 왜 우리는 Player (클래스)를 작성해야하며 player = player.get (i)를 사용할 필요가 없습니다. ? – Camus

관련 문제