2016-04-14 13 views
2

Ich mache eine Hauptmethode, die ein Array erstellt, um einige Präsidentschaftskandidaten im Auge zu behalten. Der Benutzer gibt die Anzahl der Kandidaten ein, die er möchte, und gibt dann die Kandidatennamen ein, die er bei der Wahl haben möchte, und gibt dann die Kandidaten mit der Position aus, in der sie platziert wurden (1, 2, 3, nicht 0, 1, 2). Momentan druckt die Methode nur den 2. und 3. Namen aus und druckt den ersten Namen nicht.Array druckt nicht alle Antworten korrekt

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

ich nicht einen Fehler im Code sehen .. Versuchen Sie das Debuggen .. Prüfen Sie, ob die Kandidatennamen so gespeichert sind, wie Sie es beabsichtigen. Veröffentlichen Sie auch den vollständigen Code ... –

Antwort

3

Hier ist die folgende for-Schleife das Problem

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

Was passiert, ist, auch wenn Sie fühlen Sie sich wie 5 Kandidaten eingeben, Sie haben 4 im Array tatsächlich gespeichert. Um dies zu beheben, tauschen nur die Linien wie

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

In Ihrem ersten for-Schleife Sie numCandidates -1 Werte statt numCandidates lesen, so dass Sie nur die Zeilen in Ihrer for-Schleife tauschen könnten:

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

ich denke, man kann sogar die Variable newString entfernen und verwenden Sie nur die Variable finalString:

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

Looping Problem, der Code m wie folgt sein.

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

Dies ist ein wiederkehrendes Problem. nextInt() stoppt, wenn eine Nicht-Ziffer gefunden wird, und diese Nicht-Ziffer im Eingabestream bleibt.

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

Wenn Ihr Eingangsstrom besteht aus:

3 
Alice 
Bob 
Carol 

Es sieht tatsächlich an den Scanner wie:

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

Nach nextInt() genannt wird (die 3 zurückgibt, wie erwartet), die Eingabestrom ist übrig mit:

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

Wenn nextLine() aufgerufen wird, werden alle Zeichen bis zum nächsten Zeilenvorschubzeichen gelesen und zurückgegeben, und der Zeilenumbruch wird verbraucht. Daher wird "" für newCandidate, nicht "Alice" zurückgegeben!

Wenn in einer Schleife erfolgt:

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

Die 3 Kandidaten zurückgegeben

sind:

  • ""
  • "Alice"
  • "Bob"

The mit nach Hause nehmen ist daran zu erinnern, nextLine() anzurufen Entfernen Sie nachträgliche Zeilenumbrüche, nachdem eine Zeile teilweise von anderen nextXXX()-Aufrufen gelesen wurde.

0

Zuerst haben Sie einige unnötige String-Variablen. Probieren Sie es aus, zumindest funktioniert es auf meiner Maschine!

 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: Proben

numCandidates = 3 und Namen eingegeben Unter der Annahme sind Will Linger, Andy Putty, Jimmy, sollte die Ausgabe sein "The candidates names are: 1) Will Linger 2) Andy Putty 3) Jimmy"