Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 数组的输入被“停止”;“退出”;单词_Java_Arrays_Io - Fatal编程技术网

Java 数组的输入被“停止”;“退出”;单词

Java 数组的输入被“停止”;“退出”;单词,java,arrays,io,Java,Arrays,Io,我需要做足球成绩生成器。我创建了4个数组,每个数组有10个元素,但我需要包含一个循环,允许用户改变主意,并在一定数量的条目后键入“quit”停止输入。你能帮个忙吗?我是编程新手,所以它一定非常简单 import java.util.Scanner;// public class Football_Results_Generator { public static void main(String[] args) { Scanner kbd = new Scan

我需要做足球成绩生成器。我创建了4个数组,每个数组有10个元素,但我需要包含一个循环,允许用户改变主意,并在一定数量的条目后键入“quit”停止输入。你能帮个忙吗?我是编程新手,所以它一定非常简单

import java.util.Scanner;//

public class Football_Results_Generator
{
    public static void main(String[] args) 
    {
        Scanner kbd = new Scanner (System.in);


        String[] HomeTeam = new String[10];
        String[] AwayTeam = new String[10];
        int[] HomeScore = new int[10];
        int[] AwayScore = new int[10];

        int index = 0;
        int sum = 0;
        int sum1 = 0;

        do 
        {
            System.out.print("Enter Home Team Name: ");
            HomeTeam[index] = kbd.nextLine();
            System.out.print("Enter Away Team Name: ");
            AwayTeam[index] = kbd.nextLine();
            System.out.print("Enter Home Team Score:");
            HomeScore[index] = kbd.nextInt();
            System.out.print("Enter Away Team Score: ");
            AwayScore[index] = kbd.nextInt();
            kbd.nextLine();


        } while(index < 10);
        index = 0;

        System.out.println();   

        do 
        {
            System.out.println(HomeTeam[index] + " [" + HomeScore[index] + "]" + " | " + AwayTeam[index] + " [" + AwayScore[index] + "] ");
            index = index + 1;

        } while(index < 10);

        kbd.close();

        for(index = 0; index < 10; index++)
            sum += HomeScore[index];
            for(index = 0; index < 10; index++)
                sum1 += AwayScore[index];

        System.out.println();
        System.out.println("Totals");
        System.out.println("-------------------------------");
        System.out.println("Total number of matches played: " + index);
        System.out.println("Total of all home scores: " + sum);
        System.out.println("Total of all away scores: " + sum1);
        System.out.println("Total number of draws: ");
        System.out.println("The highest home score: ");
        System.out.println("The highest away score: ");

    }
}
import java.util.Scanner//
公共级足球比赛结果生成器
{
公共静态void main(字符串[]args)
{
扫描仪kbd=新扫描仪(System.in);
String[]HomeTeam=新字符串[10];
字符串[]AwayTeam=新字符串[10];
int[]HomeScore=新int[10];
int[]AwayScore=新int[10];
int指数=0;
整数和=0;
int sum1=0;
做
{
系统输出打印(“输入主队名称:”);
HomeTeam[index]=kbd.nextLine();
系统输出打印(“输入客场球队名称:”);
AwayTeam[index]=kbd.nextLine();
系统输出打印(“输入主队得分:”);
HomeScore[index]=kbd.nextInt();
系统输出打印(“输入客场球队得分:”;
AwayScore[index]=kbd.nextInt();
kbd.nextLine();
}指数<10;
指数=0;
System.out.println();
做
{
System.out.println(HomeTeam[索引]+“[”+HomeScore[索引]+“]”+“|”+AwayTeam[索引]+“[”+AwayScore[索引]+“]);
指数=指数+1;
}指数<10;
kbd.close();
对于(索引=0;索引<10;索引++)
总和+=HomeScore[指数];
对于(索引=0;索引<10;索引++)
sum1+=AwayScore[指数];
System.out.println();
系统输出打印项次(“总计”);
System.out.println(“------------------------------------”;
System.out.println(“播放的比赛总数:+索引”);
System.out.println(“所有家庭分数的总和:+总和”);
System.out.println(“所有客场得分总和:+sum1”);
System.out.println(“提款总数:”);
System.out.println(“最高分:”);
System.out.println(“客场得分最高:”);
}
}
允许用户在5次尝试后通过键入quit来改变主意并停止输入

使用临时变量捕获字符串输入:

String line;
do 
{
    System.out.print("Enter Home Team Name: ");
    line = kbd.nextLine();
    if("quit".equalsIgnoreCase(line)){
        break;
    }
    HomeTeam[index] = line;
    .....
    index = index + 1; //missed
}while(index < 10);
index = 0;

当要求用户输入团队名称时,您希望用户能够键入“退出”,这将是他/她不想加入更多团队的信号?如果有一个团队名为“退出”,该怎么办?@Seb尝试5次后,你是什么意思?我的示例是包含10个元素的数组…我是输入团队名称和分数的用户,但同时我决定不需要填写整10个元素,我只希望存储5个元素。这就是我的意思…抱歉,伙计们,我是编程新手。谢谢你们的帮助。你能澄清一下它是如何使用的吗还有整数吗?您能用文字注释代码并解释equalsIgnoreCase(line)的作用吗?再次感谢您的帮助。数组的整数输入呢??这是同一个概念吗?从这里我知道我的下一课将是关于异常处理的。。
line = kbd.nextLine();
if("quit".equalsIgnoreCase(line)){
    break;
}
try{
    HomeScore[index] = new Integer(line);
} catch(NumberFormatException e){
    //Error conversion string to int
}