이것은이 웹 사이트의 첫 번째 게시물이므로 인내심을 바랍니다. 저는 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));
}
}
}
} 대신에 두 개의 배열을 갖는
귀하의 궁금한 점은 무엇입니까? 코드에 이미 모든 플레이어가 출석과 함께 나열되어 있습니다. 그렇지 않습니까? – Howard
최근 버전의 Java를 사용하고 있다면, 배열을 수동으로 반복하고 각 요소를 가져 오는 대신 for-each 루프 구조 (for (type x : listOfXs) {...}'를 살펴보십시오. @ dogbane의 제안에 따라 좀 더 OO-correct가되면 사소한 일이 될 것이고 상용구 코드로 인한 혼란을 크게 줄일 수있을 것입니다. –
또한, 그냥 제안하지만, 들여 쓰기 (그리고 아마도 whitespacing뿐만 아니라 수정). 당신이 일관되게 들여 쓰기한다면 (우연히 들여 쓰는 정도에 관계없이) 우연히 그 대신에 들여 쓰기하면 코드를 훨씬 쉽게 읽을 수 있습니다. –