Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/371.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/ant/2.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
螺纹“中的异常;主&x201D;java.lang.ArrayIndexOutOfBoundsException:java中出现0错误_Java - Fatal编程技术网

螺纹“中的异常;主&x201D;java.lang.ArrayIndexOutOfBoundsException:java中出现0错误

螺纹“中的异常;主&x201D;java.lang.ArrayIndexOutOfBoundsException:java中出现0错误,java,Java,有人能告诉我必须做些什么改变来消除这个错误吗?为什么会发生这种情况 public class Gambler { public static void main(String[] args) { int stake = Integer.parseInt(args[0]); // gambler's stating bankroll int goal = Integer.parseInt(args[1]); // gambler's des

有人能告诉我必须做些什么改变来消除这个错误吗?为什么会发生这种情况

public class Gambler { 

    public static void main(String[] args) {
        int stake = Integer.parseInt(args[0]);    // gambler's stating bankroll
        int goal  = Integer.parseInt(args[1]);    // gambler's desired bankroll
        int T     = Integer.parseInt(args[2]);    // number of trials to perform

        int bets = 0;        // total number of bets made
        int wins = 0;        // total number of games won

        // repeat T times
        for (int t = 0; t < T; t++) {

            // do one gambler's ruin simulation
            int cash = stake;
            while (cash > 0 && cash < goal) {
                bets++;
                if (Math.random() < 0.5) cash++;     // win $1
                else                     cash--;     // lose $1
            }
            if (cash == goal) wins++;                // did gambler go achieve desired goal?
        }

        // print results
        System.out.println(wins + " wins of " + T);
        System.out.println("Percent of games won = " + 100.0 * wins / T);
        System.out.println("Avg # bets           = " + 1.0 * bets / T);
    }

}
公共类赌徒{
公共静态void main(字符串[]args){
int stack=Integer.parseInt(args[0]);//赌徒声明资金
int goal=Integer.parseInt(args[1]);//赌徒想要的资金
int T=Integer.parseInt(args[2]);//要执行的试验数
int bets=0;//下注总数
int wins=0;//赢得的游戏总数
//重复T次
for(int t=0;t0&&现金<目标){
贝茨++;
如果(Math.random()<0.5)cash++;//赢1美元
否则现金--;//损失1美元
}
如果(现金==目标)赢了+++;//赌徒是否达到了预期目标?
}
//打印结果
系统输出打印项次(wins+“wins of”+T);
System.out.println(“赢得比赛的百分比=”+100.0*wins/T);
System.out.println(“平均下注=+1.0*下注/T”);
}
}
您可能需要验证正在接收的参数数量


您可能想看看这个:

可能是因为数组
args
中没有3个参数。。。你有没有检查过?哪一行?当你请求帮助时,最好尽可能多地提供有用的信息。。。。。。。
int stake = Integer.parseInt(args[0]);    // gambler's stating bankroll
int goal  = Integer.parseInt(args[1]);    // gambler's desired bankroll
int T     = Integer.parseInt(args[2]);    // number of trials to perform