2017-06-20 4 views
0

In meinem Programm muss ich ein Array von Strings erstellen, die es dem Benutzer ermöglichen, bis zu 100 Textzeichen nacheinander in der Konsole mit Hilfe von Scanner einzugeben. Ich brauche dann das Programm, um alle eingegebenen Zeichenketten (< = 100) anzeigen zu können, wenn der Benutzer das Wort 'stop' eintippt.String-Arrays mit Scanner

Ich erwarte nicht, dass jemand das ganze Programm für mich schreibt, aber vielleicht nur ein paar Tipps?

Ich bin sicher, das ist ziemlich einfach, aber ich bin sehr neu in Java und ich kann dieses Modul nicht scheitern, jede Hilfe wird massiv geschätzt!

package assignment2017; 

import java.util.Scanner; 

public class FootballResultsGenerator { 

    public static void main(String[] args) { 



     String m = (" : "); 
     String promptA = ("Goals scored by: "); 
     String resultA = ("Results: "); 
     String awayTeamGoals = ("the away teams scored: "); 
     String homeTeamGoals = ("the home teams scored: "); 
     String totalGoals = ("Total number of goals: "); 

       int numberOfGames=0; 

       String lineSplit = ("-------------------------------------------"); 
     //These are my teams. 

     //home 
     String ah = ("Tottenham Hotspur(Home)"); 
     String gh = ("Sunderland(Home)"); 
     String ch = ("Manchester City(Home)"); 
     String ih = ("Everton(Home)"); 
     String eh = ("Liverpool(Home)"); 
     String bh = ("Arsenal(Home)"); 
     String dh = ("Manchester United(Home)"); 
     String fh = ("Chelsea(Home)"); 
     String hh = ("West Bromwich(Home)"); 
     String jh = ("Stoke city(Home)"); 
     //away 
     String ba = ("Arsenal(Away)"); 
     String da = ("Manchester United(Away)"); 
     String fa = ("Chelsea(Away)"); 
     String ha = ("West Bromwich(Away)"); 
     String ja = ("Stoke city(Away)"); 
     String aa = ("Tottenham Hotspur(Away)"); 
     String ga = ("Sunderland(Away)"); 
     String ca = ("Manchester City(Away)"); 
     String ia = ("Everton(Away)"); 
     String ea = ("Liverpool(Away)"); 

     System.out.println("Football results generator"); 

     System.out.println(lineSplit); 

     //Tottenham 
     System.out.println (ah + " vs " + ba); 
     System.out.println(lineSplit); 
     numberOfGames++; 
     System.out.println(" -" + promptA + ah); 
     //scanner 
     Scanner scan = new Scanner(System.in); 
     int inputAH = scan.nextInt(); 


     //Arsenal 
     System.out.println(" -" + promptA + ba); 
     int inputBA = scan.nextInt(); 

     System.out.println(); 
     System.out.println (ch + " vs " + da); 
     System.out.println(lineSplit); 
     numberOfGames++; 
     //Man city 
     System.out.println(" -" + promptA + ch); 
     int inputCH = scan.nextInt(); 

     //Liverpool 
     System.out.println(" -" + promptA + da); 
     int inputDA = scan.nextInt(); 

     System.out.println(); 
     System.out.println (eh + " vs " + fa); 
     System.out.println(lineSplit); 
     numberOfGames++; 
     //chelsea 

     System.out.println(" -" + promptA + eh); 
     int inputEH = scan.nextInt(); 

     //sunderland 

     System.out.println(" -" + promptA + fa); 
     int inputFA = scan.nextInt(); 

     System.out.println(); 
     System.out.println (gh + " vs " + ha); 
     System.out.println(lineSplit); 
     numberOfGames++; 
     //West brom 

     System.out.println(" -" + promptA + gh); 
     int inputGH = scan.nextInt(); 

     //Everton 

     System.out.println(" -" + promptA + ha); 
     int inputHA = scan.nextInt(); 

     System.out.println(); 
     System.out.println (ih + " vs " + ja); 
     System.out.println(lineSplit); 
     numberOfGames++; 
     //Stoke 

     System.out.println(" -" + promptA + ih); 
     int inputIH = scan.nextInt(); 

     // 

     System.out.println(" -" + promptA + ja); 
     int inputJA = scan.nextInt(); 

     ////////////////////////////////////////////////////// second set of games 
     System.out.println (aa + " vs " + bh); 
     System.out.println(lineSplit); 
     numberOfGames++; 

     System.out.println(" -" + promptA + aa); 
     int inputAA = scan.nextInt(); 

     System.out.println(" -" + promptA + bh); 
     int inputBH = scan.nextInt(); 

     System.out.println (ca + " vs " + dh); 
     System.out.println(lineSplit); 
     numberOfGames++; 

     System.out.println(" -" + promptA + ca); 
     int inputCA = scan.nextInt(); 

     System.out.println(" -" + promptA + dh); 
     int inputDH = scan.nextInt(); 

     System.out.println (ea + " vs " + hh); 
     System.out.println(lineSplit); 
     numberOfGames++; 

     System.out.println(" -" + promptA + ea); 
     int inputEA = scan.nextInt(); 

     System.out.println(" -" + promptA + hh); 
     int inputHH = scan.nextInt(); 

     System.out.println (jh + " vs " + ga); 
     System.out.println(lineSplit); 
     numberOfGames++; 

     System.out.println(" -" + promptA + jh); 
     int inputJH = scan.nextInt(); 

     System.out.println(" -" + promptA + ga); 
     int inputGA = scan.nextInt(); 

     System.out.println (ia + " vs " + fh); 
     System.out.println(lineSplit); 
     numberOfGames++; 

     System.out.println(" -" + promptA + ia); 
     int inputIA = scan.nextInt(); 

     System.out.println(" -" + promptA + fh); 
     int inputFH = scan.nextInt(); 

     //Goals scored in total 

     int goalint = (inputAH + inputCH + inputEH + inputGH +inputIH + inputBA + inputDA + inputFA + inputHA +inputJA); 

     //Full results array 
     System.out.println(); 
     System.out.println(); 
     System.out.println(lineSplit); 
     System.out.println(resultA); 
     System.out.println(lineSplit); 
     System.out.println(); 
     //first Set Of Games 
     System.out.println(ah + m + ba + m + inputAH + m +inputBA); 
     System.out.println(ch + m + da + m + inputCH + m + inputDA); 
     System.out.println(eh + m + fa + m + inputEH + m + inputFA); 
     System.out.println(gh + m + ha + m + inputGH + m + inputHA); 
     System.out.println(ih + m + ja + m + inputIH + m + inputJA); 
     //second set of games 
     System.out.println(bh + m + aa + m + inputBH + m + inputAA); 
     System.out.println(dh + m + ca + m + inputDH + m + inputCA); 
     System.out.println(hh + m + ea + m + inputHH + m + inputEA); 
     System.out.println(jh + m + ga + m + inputJH + m + inputGA); 
     System.out.println(fh + m + ia + m + inputFH + m + inputIA); 

     System.out.println(); 
     // Goals scored... 
     System.out.println(lineSplit); 
     System.out.println(totalGoals + goalint); 
     System.out.println(homeTeamGoals + (inputAH + inputCH + inputEH + inputGH +inputIH) + " goals."); 
     System.out.println(awayTeamGoals + (inputBA + inputDA + inputFA + inputHA +inputJA) + " goals."); 
     // total number of matches 
     System.out.println("Number of games: " +numberOfGames); 

     //stop 
     String username = ("stop"); 
     String userName = ("Stop"); 

     System.exit(0); 

    }} 
+0

Haben Sie schon etwas probiert, das Sie uns zeigen können? –

+0

Teilen Sie, was Sie bis jetzt versucht haben – Akshay

+4

Ich stimme ab, diese Frage als off-topic zu schließen, weil [Fragen, die nach Hausaufgabenhilfe fragen, eine Zusammenfassung der Arbeit enthalten müssen, die Sie bis jetzt getan haben, um das Problem zu lösen, und eine Beschreibung von die Schwierigkeit, die Sie haben, es zu lösen] (https://stackoverflow.com/help/on-topic). – RealSkeptic

Antwort

3

Ich hoffe, das hilft!

String[] array = new String[100]; 
String str = null; 
Scanner sc = new Scanner(System.in); 
for (int i = 0; i < 100; i++) {  
    str= sc.nextLine(); 
    if(str.equals("stop")) { 
     break; 
    } 
    array[i] = str; 
} 
+0

Das ist eigentlich sehr hilfreich! Gibt es eine Möglichkeit, wie ich es machen kann, wenn ich 'stop' tippe, werden die vorherigen Array-Eingänge angezeigt? Vielen Dank! – JHurst

+0

@JHurst Sie können nicht erwarten, dass jemand Sie mit dem Löffel füttert! [Bitte lesen] (https://www.mkyong.com/java/java-how-to-print-an-array/). – codeRock