Java 显示多个数组变量

Java 显示多个数组变量,java,arrays,loops,delimiter,Java,Arrays,Loops,Delimiter,该程序获取2个团队和2个结果的用户输入,用“:”分隔符分隔,然后将它们存储在数组中,当用户输入单词“stop”时,它停止请求用户输入,并显示比赛的结果和统计信息(尚未添加到代码中)。我遇到的问题是,如果我键入多行匹配结果,然后键入“停止”,它只显示返回控制台的第一行用户输入,而不显示其他任何一行?输入示例:“切尔西:阿森纳:2:1” 公共静态最终字符串SENTINEL=“stop”; 公共静态void main(字符串参数[]){ 扫描仪sc=新的扫描仪(System.in); String h

该程序获取2个团队和2个结果的用户输入,用“:”分隔符分隔,然后将它们存储在数组中,当用户输入单词“stop”时,它停止请求用户输入,并显示比赛的结果和统计信息(尚未添加到代码中)。我遇到的问题是,如果我键入多行匹配结果,然后键入“停止”,它只显示返回控制台的第一行用户输入,而不显示其他任何一行?输入示例:“切尔西:阿森纳:2:1”

公共静态最终字符串SENTINEL=“stop”;
公共静态void main(字符串参数[]){
扫描仪sc=新的扫描仪(System.in);
String hometeam=新字符串();
String awayteam=新字符串();
String homescore=新字符串();
String awayscore=新字符串();
int结果0;
int结果1;
System.out.println(“请输入匹配结果:”);
//循环,将询问匹配结果(b<())
对于(int b=0;b<100;b++){
字符串s=sc.nextLine();
//停止命令
而(sc.hasNextLine()){//比for循环更好
字符串行=sc.nextLine();
字符串结果[]=s.split(“:”;//分析中间的字符串
//
用于(字符串临时:结果){
主队=结果[0];
awayteam=结果[1];
homescore=结果[2];
awayscore=结果[3];
}
//将“score”字符串转换为int值。
result0=整数.valueOf(结果[2]);
result1=整数.valueOf(结果[3]);
如果(“停止”。等于(行)){
System.out.println(Arrays.toString(results));
return;//退出
}

它输出您输入的第一个结果的原因是
results
被分配给
s.split(“:”
s
在外部for循环的第一次迭代中从不更改,因此
s.split(“:”)
从不更改。您的
结果始终保留第一个匹配结果

您的代码写得非常错误

首先,为什么在for循环中有while循环?for循环是冗余的

其次,不能使用数组进行此操作。请尝试使用
ArrayList
。数组无法动态更改其大小

第三,我建议您为此创建一个类,以表示
MatchResult

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 +
            '}';
    }
}
然后,您可以创建一个存储用户输入的
ArrayList

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));
}
Scanner sc=新扫描仪(System.in);
弦乐队;
线绳;
int homescore;
国际意识核心;
ArrayList=新建ArrayList();
System.out.println(“请输入匹配结果:”);
而(sc.hasNextLine()){//比for循环更好
字符串行=sc.nextLine();
如果(“停止”。等于(行)){
System.out.println(Arrays.toString(list.toArray());
return;//退出
}
字符串结果[]=line.split(:“”;//分析中间的字符串
主队=结果[0];
awayteam=结果[1];
homescore=Integer.valueOf(结果[2]);
awayscore=Integer.valueOf(结果[3]);
添加(新的比赛结果(hometeam、awayteam、homescore、awayscore));
}

尝试添加另一个数组

string[] matches = new string[]{};
然后将值输入数组。我使用b,因为它是循环中的int变量。我还输入了+“:”

然后将打印更改为

System.out.println(Arrays.toString(matches));
我想这应该行得通,但我没能测试它

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
            }
公共静态最终字符串SENTINEL=“stop”;
公共静态void main(字符串参数[]){
扫描仪sc=新的扫描仪(System.in);
字符串[]匹配=新字符串[]{};
String hometeam=新字符串();
String awayteam=新字符串();
String homescore=新字符串();
String awayscore=新字符串();
int结果0;
int结果1;
System.out.println(“请输入匹配结果:”);
//循环,将询问匹配结果(b<())
对于(int b=0;b<100;b++){
字符串s=sc.nextLine();
//停止命令
而(sc.hasNextLine()){//比for循环更好
字符串行=sc.nextLine();
字符串结果[]=s.split(“:”;//分析中间的字符串
//
用于(字符串临时:结果){
主队=结果[0];
awayteam=结果[1];
homescore=结果[2];
awayscore=结果[3];
}
//将“score”字符串转换为int值。
result0=整数.valueOf(结果[2]);
result1=整数.valueOf(结果[3]);
匹配[b]=hometeam.tostring()+”:“+AwayTam.tostring()+homescore.tostring()+”:“+awayscore.tostring()”;
如果(“停止”。等于(行)){
System.out.println(Arrays.toString(matches));
return;//退出
}

这里有一个简单的循环,可以从用户那里获取所有数据,直到输入“stop”并显示输入的输出

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

您在while语句的每次迭代中都声明和初始化结果,所以它总是不同的{
?我是从另一个stackoverflow问题中得到的,它起作用了,所以我把它留在了你可能是断章取义的。如果你只是复制/粘贴代码而不理解它,你永远也学不会自己编写代码。
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
            }
    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
        }
    }
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