0

現時点では、開始インデックスと終了インデックスの間の多数のプレーヤーから (ArrayList から) 属性を取得するメソッドを作成しました。これは簡単に思えますが、プロジェクトを実行しても NetBeans コンソールに何も出力されません。以下にメソッドコードを示します。

/**
 * This overloaded method will print out the details of each player - 
 * that appear between "start" and "end" indexes of the players list.
 * 
 * @param players The list of players to be printed out.
 * @param start The list position of the first player.
 * @param end The list position of the last player.
 */
public void listNPlayers(ArrayList<Player> players, int start, int end)
{
    System.out.println(csvHeader + "\n");
    int i;
    //If start is greater than 0, and end is less than the total number of players in the list
    if(start > 0 && end < players.size())
    {

        for(i = 0; (i <= end && i >= start); i++)
        {
            System.out.println(players.get(i).toString());
        }
    }
    else
    {
        //if start is less than 0, tell the user to not use a negative value
        if(start < 0)
        {
            throw new ArithmeticException("You cannot use a negative index value for 'start'.");
        }
        //if end is greater than the size of the players list, tell the user that the value is too large.
        else if(end > players.size())
        {
            throw new ArithmeticException("Your 'end' value cannot be greater than the size of your 'players' list.");
        }
    }
}

問題は for ループ領域、特にループ内の条件のどこかにあると思います。私は以前にこの条件をこのように使用したことはありませんが、それは合法であると言われています. 他の人に助けてもらいましたが、まだ何も印刷されていません。これはおそらく、私が常に見落としている非常に小さな間違いです。

プロジェクトを実行したい場合は、私のプロジェクト ファイルを GitHub ( https://github.com/rattfieldnz/Java_Projects/tree/master/PCricketStats )から複製できます。

ヒントや提案をありがとう:)。

4

3 に答える 3

3

ラインを交換することができます

for(i = 0; (i <= end && i >= start); i++)

for(i = start; i <= end; i++)

それ以来、最初のバージョンはまったく繰り返されませんstart>0i=0、終了条件i>=startはループをすぐに停止します。

于 2013-05-07T05:23:08.850 に答える
1

私はあなたが意味すると推測していますstart>=0
また、forループは次のように優れている可能性がありますfor(i = start; i <= end ; i++)

于 2013-05-07T05:24:52.800 に答える