简体   繁体   English

使用扫描仪的字符串数组

[英]String arrays using Scanner

In my program I need to create an array of strings which will allow the user to type out upto 100 strings of text one after the other in the console, using scanner. 在我的程序中,我需要创建一个字符串数组,该字符串数组允许用户使用扫描仪在控制台中一个接一个地键入最多100个文本字符串。 I then need the program to be able to display all the strings typed (<=100) when the user types the word 'stop'. 然后,我需要该程序能够在用户键入单词“ stop”时显示所有键入的字符串(<= 100)。

I'm not expecting anyone to write the whole program for me but maybe just some tips? 我不希望有人为我编写整个程序,但也许只是一些技巧?

I'm sure this is pretty basic but I'm very new to java and I cannot fail this module, any help is massively appreciated! 我敢肯定这是非常基本的,但是我对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);

    }}

I hope this helps! 我希望这有帮助!

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM