2016-04-14 14 views
2

大統領候補を追跡するための配列を作成する主な方法を作っています。ユーザは、自分が望む候補の数を入力してから、選挙で希望する候補の名前を入力して、それらが入れられた位置(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); 


} 
+0

する必要があります。..デバッグしてみ..候補者の名前が意図したとおりに保存されているかどうかを確認してください。また、完全なコードを投稿... –

答えて

3

ここでは、forループ次はあなたが5候補を入力しているように感じていても、されて何が起こる問題

for (int i = 0; i<candidates.length; i++) { 
     candidates[i] = newCandidate; 
     newCandidate = scan.nextLine();  
} 

で、あなたは実際には配列に4を格納しています。

for (int i = 0; i<candidates.length; i++) { 
    newCandidate = scan.nextLine(); 
    candidates[i] = newCandidate;  
} 

:これを解決するには、ちょうどあなたがちょうどあなたのforループ内の行を入れ替えることができるようにあなたは、numCandidates -1値の代わりに、numCandidatesを読んでいるループのためのあなたの最初で

for (int i = 0; i<candidates.length; i++) { 
    newCandidate = scan.nextLine(); 
    candidates[i] = newCandidate;  
} 
0

、としてラインを交換します

import java.util.Scanner; 

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 finalString = ""; 

     for (int i = 0; i<candidates.length; i++) { 
      newCandidate = scan.nextLine(); 
      candidates[i] = newCandidate; 
     } 

     for(int i = 0; i<candidates.length; i++) { 
      finalString += "The candidates names are: " + " " + i + ") " + candidates[i] + "\n"; 
     } 

     System.out.println(finalString); 
    } 
} 
0

ループ問題、コードM:私はあなたも変数newStringを削除し、ちょうど変数finalStringを使用することができますね次のようにしてください。

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 newString = " "; 
    String finalString = " "; 

    for (int i = 0; i<candidates.length; i++) { 
newCandidate = scan.nextLine(); 
      candidates[i] = newCandidate; 

    } 

    for(int i = 0; i<candidates.length; i++) { 
     newString = "the candidates names are: " + " " + i + ") " + candidates[i]; 
     finalString = finalString+ newString; 
    } 
    System.out.println(finalString); 


} 
0

これは再発する問題です。 nextInt()は、非数字を検出すると停止し、その非数字を入力ストリームに残します。

int numCandidates = scan.nextInt(); 
String newCandidate = scan.nextLine(); 

ご入力ストリームが構成されている場合:

3 
Alice 
Bob 
Carol 

それが実際のようにスキャナになりますnextInt()

3 \n A l i c e \n B o b \n C a r o l \n 

(予想通り、3を返す)と呼ばれ、入力ストリームは次のようになります。

\n A l i c e \n B o b \n C a r o l \n 

nextLine()が呼び出されると、次の改行文字までのすべての文字が読み込まれ、返され、改行が使用されます。よってnewCandidateでは""が返され、"Alice"では返されません!

ループで行われた場合:

int numCandidates = scan.nextInt(); 
for(int i=0; i<numCandidates; i++) { 
    String newCandidate = scan.nextLine(); 
    //... 
} 

返さ3候補者は次のとおりです。

  • ""
  • "アリス"
  • "ボブ"

帰宅するにはnextLine()に電話することを忘れないでください他のnextXXX()呼び出しによって行が部分的に読み取られた後に、改行文字を削除します。

0

まず、不要な文字列変数があります。これを試してみてください。少なくとも私のマシンではうまくいきます!

 String[] candidates = new String [numCandidates]; 
     System.out.println("Please input the name of the candidates in the election, one on each line"); 
     Scanner scan = new Scanner(System.in); 
     StringBuilder finalCandidates = new StringBuilder(); 

     for (int i = 0; i < candidates.length; i++) { 
      candidates[i] = scan.nextLine(); 
     } 
     scan.close(); 

     for(int i = 0; i < candidates.length; i++) { 
      finalCandidates.append(i + 1 + ") " + candidates[i] + " "); 
     } 

     System.out.println("The candidates names are: " + " " + finalCandidates.toString()); 

EDIT:サンプル

が入っnumCandidates = 3と名前を仮定がWill Linger, Andy Putty, Jimmyあり、出力は私はコード内のエラーを参照してくださいいけない "The candidates names are: 1) Will Linger 2) Andy Putty 3) Jimmy"

関連する問題