- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
<分区>
我正在尝试创建一个二十一点游戏。
首先,我使用类构造函数创建了一个名为 deck size 52 的数组。
public Deck(){
for(int i=0; i<13; i++){ //Spades
if(i>9){
deck[i]=10;
}
else{
deck[i]=(i+1);
}
}
for(int i=13; i<26; i++){ //Hearts
if(i>21){
deck[i]=10;
}
else{
deck[i]=(i-12);
}
}
for(int i=26; i<39; i++){ //Clubs
if(i>34){
deck[i]=10;
}
else{
deck[i]=(i-25);
}
}
for(int i=39; i<52; i++){ //Diamonds
if(i>47){
deck[i]=10;
}
else{
deck[i]=(i-38);
}
}
}
然后,我还在 Deck 类中创建了一个 shuffleDeck 函数。
public void shuffleDeck(){
int[] temp = new int[52];
int[] indexChecker = new int[52];
for(int i=0 ; i<52 ; i++){
indexChecker[i]=0;
}
int index = 0;
for(int i=0; i<52 ; i++){
index = number.nextInt(52);
for(i=0; i<52 ; i++){
while(index == indexChecker[i])
index = number.nextInt(52);
}
temp[i] = deck[index]; //The error is here
}
for(int i=0; i<52; i++){
deck[i] = temp [i];
}
}
我使用这个算法来确保我的随机索引生成器不会生成相同的数字来洗牌。
但是当我尝试在主类中对其进行随机播放时,它不起作用。
public static void main(String[] args) {
Deck card = new Deck();
System.out.println("Decks are created");
card.getDeck();
System.out.println("Shuffling...");
card.shuffleDeck();
System.out.println("Shuffled deck.");
card.getDeck();
输出:
run:
Decks are created
0-1
1-2
2-3
3-4
4-5
5-6
6-7
7-8
8-9
9-10
10-10
11-10
12-10
13-1
14-2
15-3
16-4
17-5
18-6
19-7
20-8
21-9
22-10
23-10
24-10
25-10
26-1
27-2
28-3
29-4
30-5
31-6
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 52
32-7
33-8
34-9
35-10
36-10
37-10
38-10
39-1
40-2
at blackjack.pkg5.pkg0.Deck.shuffleDeck(Deck.java:69)
41-3
42-4
43-5
44-6
45-7
at blackjack.pkg5.pkg0.Blackjack50.main(Blackjack50.java:13)
46-8
47-9
48-10
49-10
50-10
51-10
Shuffling...
C:\Users\Afrie Irham\AppData\Local\NetBeans\Cache\8.2\executor-snippets\run.xml:53: Java returned: 1
BUILD FAILED (total time: 0 seconds)
我正在研究合并排序,并且抛出了ArrayIndexOutOfBoundsException。我一直在梳理代码寻找错误,但找不到它。异常堆栈跟踪表明它来自第 77 行的合并函数,即 temp[curre
我在测试 arrayOutOfBounds 异常时遇到了问题。在下面的代码中,我的 if...else 语句应该阻止骑士离开我的棋盘,但我仍然遇到异常。有人在这里看到我的错误吗?感谢您的帮助! pub
这个问题在这里已经有了答案: How can I avoid ArrayIndexOutOfBoundsException or IndexOutOfBoundsException? [duplica
我正在尝试实现一个通用的合并排序算法,该算法使用临时数组来存储合并的部分,然后复制排序的数据。但是,程序在复制步骤(最后一个 while 循环)中不断失败,并抛出 ArrayIndexOutOfBou
我正在尝试使用快速排序方法来对部分或部分数据进行排序。我认为我的分区方法是正确的,我对左段和右段进行快速排序的递归调用给了我一个数组越界异常,但我似乎无法弄清楚。这是我编写的代码,并且在 中收到错误
我创建了一个写入文件的方法,如下面的代码所示。使用两个给定参数从另一个方法调用此方法 1000 次。 但是,在其中 5 个调用中,此方法捕获错误“e”并打印出: java.lang.ArrayInde
我有这些 while 循环和 for 语句。它正在做我想要的事情,打印数组的特定部分。但是,我不断收到越界声明。我想不通。我觉得我的 if 语句应该防止索引变得更大。 注意:除了创建此数组并初始化其值
这个问题在这里已经有了答案: What causes a java.lang.ArrayIndexOutOfBoundsException and how do I prevent it? (24
此代码是我的康威生命游戏模拟代码的一部分。这种特殊的方法是一个“步骤”,将游戏向前推进了一代人。当它尝试检查直方图是真还是假时似乎出现错误(目前这是一个 10 x 10 boolean 数组)。该代码
我正在尝试在java中实现合并排序,并且我已经按照CLRS书中给出的算法编写了代码。当我尝试运行代码时,我继续遇到数组越界异常。老实说,我不明白我在这里犯了什么错误。 package mergesor
虽然这段代码在没有添加 GUI 的情况下工作正常。我现在遇到以下异常。 (我读到此类异常的 SCCE 发布了整个堆栈跟踪以及堆栈跟踪第一行的代码。我希望它很清楚。) 错误: Exception
这个问题已经有答案了: What causes a java.lang.ArrayIndexOutOfBoundsException and how do I prevent it? (25 个回答)
我的代码编译没有错误,但在我的输出中,第 37 行出现 ArrayOutofBoundsException。除了素数计数器之外,一切正常。谁能看到我在这段代码中犯了错误吗?主计数器在我的另一个程序中工
我似乎遇到了一个问题,当我访问超出数组范围的索引时,我的编译器没有正确给出异常。 例如:在我的代码中我有 int reds[8][8]; 这应该创建一个高度为 8(0-7)、宽度为 8(0-7) 的二
我一直在浏览很多关于这个的网站,并尝试了一些不同的东西,但我被难住了。将不胜感激一些帮助。条件正常,检查是否选中了一个复选框(真),但是当我执行 model.removeRow(row) 时,它会给我
我正在使用 Commonsware 的 EndlessAdapter。当我添加了一个 header View 时,我得到了我的 listView,我得到了这个错误和 LogCat: 04-22 17:
在开发我的第一个“大”Spring 项目时,我陷入了一个无法找到任何反馈的事情。我尝试列出所有用户 - 问题出现在 UserServiceJpaImpl 类中的 findAll 方法中(它只从 Jpa
我很难在 Opensuse 13.2 上安装可用的 Android Studio。 我已经尝试过 Java OpenSDK 1.7.0_75-b13 和 1.8.0_40-b10 以及 Oracle
我正在努力完成一项任务,其中必须将文本文件中的数据存储到 ArrayList 中,其中包括歌曲描述的字符串值以及持续时间和评级值的 double 值。 很抱歉造成困惑,我是这个网站的新手。 这是我尝试
我正在使用 GUI 进行用户名/密码登录。我使用 while 循环,将文件名扩展为 hasNext() 作为将文件中的信息存储到数组中的条件,以便我可以将用户的输入与文件中存储的信息进行比较。然后我使
我是一名优秀的程序员,十分优秀!