大統領候補を追跡するための配列を作成する主な方法を作っています。ユーザは、自分が望む候補の数を入力してから、選挙で希望する候補の名前を入力して、それらが入れられた位置(1,2,3は0,1,2)ではなく候補をプリントアウトする。今のところ、このメソッドは2番目と3番目の名前だけを出力し、最初の名前は印刷しません。配列がすべての回答を正しく出力しない
public class RunElection
/**
*
*/
public static void main(String[] args)
{
Scanner scan = new Scanner(System.in);
System.out.println("Please input the number of candidates you would like in this election.");
int numCandidates = scan.nextInt();
while (numCandidates <= 0) {
System.out.println("Please input a number that is greater than zero.");
numCandidates = scan.nextInt();
}
String[] candidates;
candidates = new String[numCandidates];
System.out.println("Please input the name of the candidates in the election.");
String newCandidate = scan.nextLine();
String newString = " ";
String finalString = " ";
for (int i = 0; i<candidates.length; i++) {
candidates[i] = newCandidate;
newCandidate = scan.nextLine();
}
for(int i = 0; i<candidates.length; i++) {
newString = "the candidates names are: " + " " + i + ") " + candidates[i];
finalString = finalString+ newString;
}
System.out.println(finalString);
}
する必要があります。..デバッグしてみ..候補者の名前が意図したとおりに保存されているかどうかを確認してください。また、完全なコードを投稿... –