【问题标题】:Incrementing array values java [closed]递增数组值java [关闭]
【发布时间】:2024-01-21 03:15:01
【问题描述】:

我正在编写一个程序,允许用户输入运动队,然后根据他们输入的球队记录输赢。我相当肯定我设置数组的方式是错误的,因为它没有正确增加胜负,有人看到问题了吗?

import java.util.Scanner;

public class sports {


public static void main(String[] args) {


    System.out.println("Howdy sports fan!");

    String menuSelect;
    String winSelect;
    String loseSelect;
    int teamSize = 0;

    String[] teamsArray = new String[0];
    int[] winsArray = new int[0];
    int[] lossesArray = new int[0];


    do {

        System.out.println("Please pick an option from the list below:");
        System.out.println("1) Create League");
        System.out.println("2) List all teams");
        System.out.println("3) Record a win");          
        System.out.println("4) Record a loss");         
        System.out.println("5) Quit");          
        Scanner keyboard = new Scanner(System.in);
        menuSelect = keyboard.nextLine();

        if ( menuSelect.equals("1") )
        {

            System.out.println("How many teams should I make?");
            try
            {
            teamSize = Integer.parseInt(keyboard.nextLine());
            }
            catch (NumberFormatException e)
            {
                System.out.println("Invalid entry, try again.");
            }

            teamsArray = new String[teamSize];

            for ( int i = 0; i < teamsArray.length; ++i )
            {
                System.out.println("Team " + (i+1) + "'s name?");
                teamsArray[i] = keyboard.nextLine();                
            }
        }

        else if ( menuSelect.equals("2") )
        {

            if (teamsArray.length == 0)
            {
                System.out.println("There are no teams!");
            }
            else


            System.out.printf("%21s %21s %n", "W", "L");

            for ( int i = 0; i < teamsArray.length; ++i )
            {
                System.out.printf(teamsArray[i] + "%20d %21d %n", winsArray[i], lossesArray[i]);

            }
        }

        else if ( menuSelect.equals("3") )
        {
            winsArray = new int[teamSize];
            System.out.println("Which team won a game?");
            winSelect = keyboard.nextLine();

            for ( int i = 0; i < teamsArray.length; ++i )
            {
                if ( winSelect.equals(teamsArray[i]) )
                {
                    ++winsArray[i];
                }
            }
        }

        else if ( menuSelect.equals("4") )
        {
            lossesArray = new int[teamSize];
            System.out.println("Which team lost a game?");
            loseSelect = keyboard.nextLine();

            for ( int i = 0; i < teamsArray.length; ++i )
            {
                if ( loseSelect.equals(teamsArray[i]) )
                {
                    ++lossesArray[i];
                }
            }
        }
        else
        {
            System.out.println("Invalid Selection");
        }


    } while(!menuSelect.equals("5"));

}

}

【问题讨论】:

  • 什么是输入,什么是预期结果,什么是实际结果?你用过调试器吗?

标签: java arrays increment


【解决方案1】:

您必须一次初始化 Option#1 中的所有数组,而不是像这样在 Option#3/4 中初始化。

import java.util.Scanner;

public class sports {


public static void main(String[] args) {


    System.out.println("Howdy sports fan!");

    String menuSelect;
    String winSelect;
    String loseSelect;
    int teamSize = 0;

    String[] teamsArray = new String[0];
    int[] winsArray = new int[0];
    int[] lossesArray = new int[0];


    do {

        System.out.println("Please pick an option from the list below:");
        System.out.println("1) Create League");
        System.out.println("2) List all teams");
        System.out.println("3) Record a win");          
        System.out.println("4) Record a loss");         
        System.out.println("5) Quit");          
        Scanner keyboard = new Scanner(System.in);
        menuSelect = keyboard.nextLine();

        if ( menuSelect.equals("1") )
        {

            System.out.println("How many teams should I make?");
            try
            {
            teamSize = Integer.parseInt(keyboard.nextLine());
            }
            catch (NumberFormatException e)
            {
                System.out.println("Invalid entry, try again.");
            }

            teamsArray = new String[teamSize];
            winsArray = new int[teamSize];
            lossesArray = new int[teamSize];

            for ( int i = 0; i < teamsArray.length; ++i )
            {
                System.out.println("Team " + (i+1) + "'s name?");
                teamsArray[i] = keyboard.nextLine();                
            }
        }

        else if ( menuSelect.equals("2") )
        {

            if (teamsArray.length == 0)
            {
                System.out.println("There are no teams!");
            }
            else


            System.out.printf("%21s %21s %n", "W", "L");

            for ( int i = 0; i < teamsArray.length; ++i )
            {
                System.out.printf(teamsArray[i] + "%20d %21d %n", winsArray[i], lossesArray[i]);

            }
        }

        else if ( menuSelect.equals("3") )
        {
            System.out.println("Which team won a game?");
            winSelect = keyboard.nextLine();

            for ( int i = 0; i < teamsArray.length; ++i )
            {
                if ( winSelect.equals(teamsArray[i]) )
                {
                    ++winsArray[i];
                }
            }
        }

        else if ( menuSelect.equals("4") )
        {
            System.out.println("Which team lost a game?");
            loseSelect = keyboard.nextLine();

            for ( int i = 0; i < teamsArray.length; ++i )
            {
                if ( loseSelect.equals(teamsArray[i]) )
                {
                    ++lossesArray[i];
                }
            }
        }
        else
        {
            System.out.println("Invalid Selection");
        }


    } while(!menuSelect.equals("5"));

}

}

【讨论】:

  • 不要通过编写完整的代码来帮助这个人。告诉他他的错误并让他改正。如果给出这样一个完整的代码,这个人不会知道他的错误是什么。
【解决方案2】:

问题在于,每当您读取输赢时,您都会分配winsArraylossesArray。以下两条语句应在选项 1 中:

winsArray = new int[teamSize];
lossesArray = new int[teamSize];

【讨论】:

    【解决方案3】:

    如果你像这样初始化数组,编译器认为这三个变量是teamArray, winsArray 和 lossArray 是大小为零的数组类型。这意味着您可以在数组中添加任何值,当您尝试访问像 teamsArray[i] 时会导致错误。

    String[] teamsArray = new String[0];
    int[] winsArray = new int[0];
    int[] lossesArray = new int[0];
    

    因此,当用户按照 ravidra 所说的方式选择选项一来创建联赛时,请尝试初始化数组。

    teamsArray = new String[teamSize];
    winsArray = new int[teamSize];
    lossesArray = new int[teamSize];
    

    尝试了解哪里出了问题以及如何纠正它。

    【讨论】:

    • 我是来学习的,有些事情可能会令人困惑,但我知道在这种情况下出了什么问题。感谢您的帮助