gpt4 book ai didi

java - 掷骰子 - ArrayIndexOutOfBounds

转载 作者:太空宇宙 更新时间:2023-11-04 13:56:56 25 4
gpt4 key购买 nike

我刚刚开始学习 Java 编程,并编写了一个程序来掷 x 面骰子 x 次。边数和卷数由用户输入定义。该程序以 JTable 格式给出每个数字的绝对频率和相对频率。一切都很顺利,直到您为侧面和卷数选择较大的数字。我收到 ArrayIndexOutOfBoundsException 但在代码中找不到任何相应的错误。

package rolling;

import java.awt.*;
import java.util.*;
import javax.swing.*;
import javax.swing.table.TableCellRenderer;
import javax.swing.JOptionPane;

public class RollDice extends JPanel {

private static final long serialVersionUID = -6332129624300946462L;
JTable jt;
int i, k, j; //Counter for loops

/*========== CONSTRUCTOR CREATES TABLE OBJECT ==========*/
public RollDice(int[] trial, int[] outcomes, int[] dice_numbers,
int[] count, float[] Rel_frequencies){

String[] columnNames = {"Number of trial", "Outcome", "Dice Numbers",
"Absolute Frequencies", "Relative Frequencies"};

Object[][] input = new Object[trial.length][columnNames.length];

for (i=0; i<trial.length; i++){
input[i][0] = trial[i];
}
for (i=0; i<outcomes.length; i++){
input[i][1] = outcomes[i];
}
for (i=0; i<dice_numbers.length; i++){
input[i][2] = dice_numbers[i];
}
for (i=0; i<count.length; i++){
input[i][3] = count[i];
}
for (i=0; i<Rel_frequencies.length; i++){
input[i][4] = Rel_frequencies[i];
}

/*Checking the outcome!
for (i=0; i<trial.length; i++){
System.out.println();
for (k=0; k<columnNames.length; k++){
System.out.printf("%d\t", input[i][k]);
}
}*/
jt = new JTable(input,columnNames)
{
/**
*
*/
private static final long serialVersionUID = 1L;

public boolean isCellEditable(int input, int columns)
{
return false;
}
public Component prepareRenderer(TableCellRenderer r, int input,
int columns){
Component c = super.prepareRenderer(r, input, columns);

if (input %2 == 0){
c.setBackground(Color.WHITE);
}
else{
c.setBackground(Color.LIGHT_GRAY);
}
if (isCellSelected(input, columns)){
c.setBackground(Color.YELLOW);
}
return c;
}
};

jt.setPreferredScrollableViewportSize(new Dimension(450, 600));
jt.setFillsViewportHeight(true);

JScrollPane jps = new JScrollPane(jt);
add(jps);
}

public static int[] roll_dice(int sides, int rolls){
int[] outcomes = new int[rolls];
int i; //Counter for accessing array position (element)
for (i=0; i<rolls; i++){
outcomes[i] = (int)(1 + Math.random() * sides);
}
return outcomes;
}

public static int[] Frequency_count(int[] outcomes, int sides){
int[] count = new int[sides];
int i;
int j, k = 0;
for(i=1; i<=sides; i++){
for(j=0; j<outcomes.length; j++){
if (outcomes[j] == i){
count[k]++;
}
}
//System.out.printf("%d \t %d\n",i, count[k]);
k++;
}
return count;
}

public static float[] Relative_frequencies(int[] count, int sides,
int rolls){
int i;
float[] Array = new float [sides];
for(i=0; i<sides; i++){
Array[i] = (float)count[i] / rolls * 100;
//String.format("%.3f", (float)Array[i]);
//System.out.printf("%d \t %.2f\n",i+1, Array[i]);
}
return Array;
}

public static void main(String[] args) {
/*=========USER INPUT via GUI: DECISION ON HOW MANY TIMES
THE DICE IS ROLLED===*
*========= AND HOW MANY SIDES THE DICE HAS ===*/

String fn = JOptionPane.showInputDialog("Enter the number of sides
of the dice");
String sn = JOptionPane.showInputDialog("Enter the number of rolls");

int sides = Integer.parseInt(fn); // number of sides
int rolls = Integer.parseInt(sn); // number of rolls

JOptionPane.showMessageDialog(null, "You rolled a " + sides + "
sided dice " + rolls + " times!", "User Input",
JOptionPane.INFORMATION_MESSAGE );

/*=========GENERATING RANDOM NUMBERS (ROLLING THE DICE) WITH
"roll_dice" method==========
*========= AND COUNTING THE NO. OF TRIALS ==========*/
int[] outcomes = roll_dice(sides, rolls);
int[] trial = new int[rolls];
int[] dice_numbers = new int[sides];

int i, k;
k = 1;
for(i=0; i<rolls; i++){
trial[i] = k;
//System.out.println(i + " " + k);
k++;
}
k = 1;
for(i=0; i<sides; i++){
dice_numbers[i] = k;
//System.out.println(i + " " + k);
k++;
}
/*=========COUNTING THE FREQUENCIES OF EACH NUMBER==========*/
//System.out.println("ABSOLUTE Frequencies plotted in FUNCTION:");
int[] count = Frequency_count(outcomes, sides);
//System.out.println("RELATIVE Frequencies plotted in FUNCTION:");
float[] Rel_frequencies = Relative_frequencies(count, sides, rolls);


/*=========CREATING A TABLE FORMAT WITH A JAVA
LIBRARY (JTABLE)==========*/
JFrame jf = new JFrame();
RollDice table1 = new RollDice(trial, outcomes, dice_numbers,
count, Rel_frequencies);
jf.setTitle("Absolute and Relative Frequencies of numbers for
an arbitrary Dice");
jf.setSize(500, 700);
jf.setVisible(true);
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jf.add(table1);
}
}

最佳答案

看看这部分代码:

Object[][] input = new Object[trial.length][columnNames.length];

for (i=0; i<trial.length; i++){
input[i][0] = trial[i];
}
for (i=0; i<outcomes.length; i++){
input[i][1] = outcomes[i];
}
for (i=0; i<dice_numbers.length; i++){
input[i][2] = dice_numbers[i];
}
for (i=0; i<count.length; i++){
input[i][3] = count[i];
}
for (i=0; i<Rel_frequencies.length; i++){
input[i][4] = Rel_frequencies[i];
}

您将input 2D 数组设置为特定大小,但随后您不假思索地访问它。

例如,看看这个:

int[] trial = new int[rolls];
int[] dice_numbers = new int[sides];

假设用户掷 2 个骰子,得到 6 面骰子(传统骰子)。然后,例如,在第三个循环中,您访问 input[i][2],其中 i 从 0 运行到 5,但是由于 Trial.length = 2,您正在访问不存在的索引。

您应该检查您的代码,以便仅根据初始化期间给定的边界访问您的数组(我对您的任务了解不够,无法尝试提出更好的建议)。

关于java - 掷骰子 - ArrayIndexOutOfBounds,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29729505/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com