2017-06-26 3 views
-1

Dieses Programm erhält Benutzereingaben für 2 Teams und 2 Ergebnisse, trennt sie mit dem Trennzeichen ":" und speichert sie im Array. Wenn der Benutzer das Wort "stop" eingibt, wird keine Benutzereingabe mehr angefordert und soll die Ergebnisse und Statistiken des Spiels anzeigen (die noch nicht in den Code eingefügt wurden). Das Problem, das ich habe, ist, wenn ich mehr als eine Zeile der Übereinstimmungsergebnisse eintippe und dann 'stop' tippe, es zeigt nur die erste Linie der Benutzereingabe zurück zur Konsole und nicht irgendwelche der anderen an? Eingabebeispiel: "Chelsea: Arsenal: 2: 1".Anzeige mehrerer Array-Variablen

public static final String SENTINEL = "stop"; 

public static void main(String args[]) { 

    Scanner sc = new Scanner(System.in); 

    String hometeam = new String(); 
    String awayteam = new String(); 
    String homescore = new String(); 
    String awayscore = new String(); 

    int result0; 
    int result1; 

    System.out.println("please enter match results:"); 

    // loop, wil ask for match results (b <()) 
    for (int b = 0; b < 100; b++) { 

     String s = sc.nextLine(); 

     // stop command 
     while (sc.hasNextLine()) { // better than the for loop 

      String line = sc.nextLine(); 

      String results[] = s.split(" : "); // parse strings in between 
               // the 

      for (String temp : results) { 
       hometeam = results[0]; 
       awayteam = results[1]; 
       homescore = results[2]; 
       awayscore = results[3]; 
      } 

      // convert 'score' strings to int value. 
      result0 = Integer.valueOf(results[2]); 
      result1 = Integer.valueOf(results[3]); 

      if ("stop".equals(line)) { 
       System.out.println(Arrays.toString(results)); 
       return; // exit 
      } 
+1

Du deklarieren und Initialisieren ** ergibt ** bei jeder Iteration von ** while ** Anweisung, so ist es immer anders. – luizfzs

+0

Warum 'for (String temp: results) {'? – Yonlif

+0

Ich habe das aus einer anderen stackoverflow Frage und es hat funktioniert, also habe ich es einfach in –

Antwort

0

Der Grund, dass es die ersten Ergebnisse gibt Ihnen eingegebene weil results zu s.split(" : ") zugeordnet ist. s ändert sich nie in der ersten Iteration der äußeren for-Schleife, so ändert sich s.split(" : ") nie. Deine results hält immer die ersten Treffer!

Sie haben Ihren Code sehr falsch geschrieben.

Erstens, warum haben Sie eine while-Schleife innerhalb einer for-Schleife? Die for-Schleife ist redundant.

Zweitens können Sie Arrays dafür nicht verwenden. Versuchen Sie eine ArrayList. Arrays können ihre Größe nicht dynamisch ändern.

Drittens empfehle ich Ihnen, eine Klasse dafür zu erstellen, um eine MatchResult darzustellen.

class MatchResult { 
    private String homeTeam; 
    private String awayTeam; 
    private int homeScore; 
    private int awayScore; 

    public String getHomeTeam() { 
     return homeTeam; 
    } 

    public String getAwayTeam() { 
     return awayTeam; 
    } 

    public int getHomeScore() { 
     return homeScore; 
    } 

    public int getAwayScore() { 
     return awayScore; 
    } 

    public MatchResult(String homeTeam, String awayTeam, int homeScore, int awayScore) { 
     this.homeTeam = homeTeam; 
     this.awayTeam = awayTeam; 
     this.homeScore = homeScore; 
     this.awayScore = awayScore; 
    } 

    @Override 
    public String toString() { 
     return "MatchResult{" + 
      "homeTeam='" + homeTeam + '\'' + 
      ", awayTeam='" + awayTeam + '\'' + 
      ", homeScore=" + homeScore + 
      ", awayScore=" + awayScore + 
      '}'; 
    } 
} 

Dann können Sie eine ArrayList<MatchResult> erstellen, die den Benutzereingaben speichert.

Scanner sc = new Scanner(System.in); 

String hometeam; 
String awayteam; 
int homescore; 
int awayscore; 
ArrayList<MatchResult> list = new ArrayList<>(); 

System.out.println("please enter match results:"); 
while (sc.hasNextLine()) { // better than the for loop 

    String line = sc.nextLine(); 
    if ("stop".equals(line)) { 
     System.out.println(Arrays.toString(list.toArray())); 
     return; // exit 
    } 
    String results[] = line.split(" : "); // parse strings in between 

    hometeam = results[0]; 
    awayteam = results[1]; 
    homescore = Integer.valueOf(results[2]); 
    awayscore = Integer.valueOf(results[3]); 

    list.add(new MatchResult(hometeam, awayteam, homescore, awayscore)); 
} 
0

Versuchen Sie, nur ein weiteres Array

string[] matches = new string[]{}; 

dann geben Sie Ihre Werte in das Array. Ich verwende b, da dies die int-Variable in Ihrer Schleife ist. Ich habe auch in + „:“

matches [b] = hometeam.tostring() + " : " + awayteam.tostring() + homescore.tostring() + " : " + awayscore.tostring(); 

dann den Druck ändern

System.out.println(Arrays.toString(matches)); 

ich denke, das sollte funktionieren, aber ich war nicht in der Lage, es zu testen.

public static final String SENTINEL = "stop"; 

public static void main(String args[]) { 

    Scanner sc = new Scanner(System.in); 
    string[] matches = new string[]{}; 
    String hometeam = new String(); 
    String awayteam = new String(); 
    String homescore = new String(); 
    String awayscore = new String(); 

    int result0; 
    int result1; 

    System.out.println("please enter match results:"); 

    // loop, wil ask for match results (b <()) 
    for (int b = 0; b < 100; b++) { 

     String s = sc.nextLine(); 

     // stop command 
     while (sc.hasNextLine()) { // better than the for loop 

      String line = sc.nextLine(); 

      String results[] = s.split(" : "); // parse strings in between 
               // the 

      for (String temp : results) { 
       hometeam = results[0]; 
       awayteam = results[1]; 
       homescore = results[2]; 
       awayscore = results[3]; 
      } 

      // convert 'score' strings to int value. 
      result0 = Integer.valueOf(results[2]); 
      result1 = Integer.valueOf(results[3]); 

      matches [b] = hometeam.tostring() + " : " + awayteam.tostring() + homescore.tostring() + " : " + awayscore.tostring(); 

      if ("stop".equals(line)) { 
       System.out.println(Arrays.toString(matches)); 
       return; // exit 
      } 
0

Hier ist eine einfache Schleife alle Daten vom Benutzer, bis „Stop“ greifen eingegeben und die Ausgabe des Eingabeanzeige

Scanner sc = new Scanner(System.in); 
    ArrayList<String[]> stats = new ArrayList<>(); //initialize a container to hold all the stats 
    System.out.println("please enter match results:"); 
    while(sc.hasNextLine()) 
    { 
     String input = sc.nextLine(); 
     String[] results = input.split(" : "); 
     if(results.length == 4) 
     { 
      stats.add(results); 
     } 
     else if(input.equals("stop")) 
      break; 
     else 
      System.out.println("Error reading input"); 
    }//end of while 

    for(int i = 0; i < stats.size(); i++) 
    { 
     try{ 
      System.out.println(stats.get(i)[0] + " vs " + stats.get(i)[1] + " : " + 
       Integer.valueOf(stats.get(i)[2]) + " - " + Integer.valueOf(stats.get(i)[3])); 
     }catch (Exception e) { 
      //do nothing with any invalid input 
     } 
    } 

Output

please enter match results: 
r : b : 5 : 4 
r : c : 7 : 10 
j : g : 3 : 9 
stop 
r vs b : 5 - 4 
r vs c : 7 - 10 
j vs g : 3 - 9 
Verwandte Themen