gpt4 book ai didi

java - 使用 Scanner 的字符串数组

转载 作者:行者123 更新时间:2023-12-01 16:48:52 26 4
gpt4 key购买 nike

在我的程序中,我需要创建一个字符串数组,该数组允许用户使用扫描仪在控制台中一个接一个地输入最多 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);

}}

最佳答案

我希望这有帮助!

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

关于java - 使用 Scanner 的字符串数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44649688/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com