使用Scanner的字符串数组

时间:2017-06-20 09:51:29

标签: java arrays string java.util.scanner

在我的程序中,我需要创建一个字符串数组,允许用户使用scanner在控制台中一个接一个地输出最多100个文本字符串。然后我需要程序能够在用户输入单词'stop'时显示所有输入的字符串(< = 100)。

我不希望任何人为我编写整个程序,但可能只是一些提示?

我确信这是非常基本的,但我对java很新,我不能让这个模块失败,任何帮助都会受到大力赞赏!

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);

    }}

1 个答案:

答案 0 :(得分:5)

我希望这有帮助!

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;
}