- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在为学校做一个项目,我们必须多次旋转一个立方体,并找到从立方体收到的数字的最长运行。我几乎完成了代码,它符合要求,但每当我运行它时,我都会遇到同样的错误:
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 0
at number_cube.number_cube.cubeToss(number_cube.java:20)
at number_cube.number_cube.main(number_cube.java:10)
谁能帮我解决这个问题?
这是我的代码:
public class number_cube {
public static int ans;
public static void main(String[]args){
Scanner scan = new Scanner(System.in);
System.out.println("How many times would you like to toss the cube?");
ans = scan.nextInt();
cubeToss();
getLongestRun();
System.out.println();
}
public static int arr[] = new int[ans];
public static int[] cubeToss(){
for(int i = 0; i < ans; i++){
int randnum = (int) (1 + Math.random() * (6-1));
arr[i] = randnum;
}
return arr;
}
public static void getLongestRun(){
int longest = 0;
int length = 1;
for(int i = 1; i < ans; i++)
if(arr[i] == arr[i-1]){
length++;
}
else{
length = 1;
}
if(length > longest){
longest = length;
}
System.out.println("The longest run is " + longest + ".");
}
}
最佳答案
您使用变量 ans
作为您的 arr
数组的长度,但是在创建数组时,ans
没有值(Java 将其初始化为 0),因此 arr
数组的长度为 0。main
方法在类初始化之前不会被调用;在调用 main
方法之前初始化所有 static
变量。
在获得有效长度之前不要创建数组:
ans = scan.nextInt();
arr = new int[ans]; // Add this line.
cubeToss();
关于java - 如何修复数组 OutOfBoundsException?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20225464/
我有一个 ArrayIndexOutOfBoundsException 问题,它总是出现在我的程序中。我怎样才能进入 try{} ? @Override public Object pop()
这个问题已经有答案了: Are fields initialized before constructor code is run in Java? (5 个回答) 已关闭 7 年前。 我(相对)是j
do{ System.out.println("inside do"); for (int i = 0; i < i2; i++) { Str
我正在为学校做一个项目,我们必须多次旋转一个立方体,并找到从立方体收到的数字的最长运行。我几乎完成了代码,它符合要求,但每当我运行它时,我都会遇到同样的错误: Exception in thread
我正在编写一个应用许多计算语言学原理的程序。我现在的问题是下面的一段代码形成了一个“灵活化两个定义”的方法。也就是说,它比较同一个词的两个不同定义,并且在每个定义中将添加空格或空格,以便稍后使用更改后
我尝试在 Symfony 2 中创建一个自定义身份验证提供程序,只是为了好玩。我关注这个文档 http://symfony.com/doc/current/cookbook/security/cust
假设我有一个方法接受 0 到 x 之间的值。如果参数大于 x 或小于零,我想抛出异常。 OutOfBoundsException 是抛出的合理异常吗? http://php.net/manual/en
嘿,我有一个问题,为什么我的程序会抛出 ArrayIndextOutofBounds 异常我在互联网上到处查看并浏览了代码,但我知道我遗漏了一些东西。这是我第一个实现了堆栈的程序,坦率地说,我并不完全
我正在尝试创建一个 RecyclerView,它根据 Firebase 中的数据填充 CardView。当 Firebase 中没有数据时,我收到 IndexOutOfBoundsException,
我正在编写一个程序来删除单词中的重复字母,但它在第 22 行显示越界错误,我无法弄清楚问题是什么。代码如下 import java.io.*; public class p23 { publi
我的代码有问题。我正在编写一个必须管理每周深蹲训练的程序。可以记录之前的训练或安排一整周的训练。在第二种情况下,用户必须输入每天的深蹲次数和他认为的难度。我用一个有 3 种声音(简单、中等、困难)的选
我正在使用 ListView 来呈现类似 GridView 的 Activity 。存在 OutOfBoundsException。这是日志。 03-21 11:47:13.106: E/Androi
我收到以下错误: Exception in thread "main" java.lang.IndexOutOfBoundsException: Index: 86, Size: 86 at java
如果对象 LimitIterator 为空,我如何编写一个不运行下面的 foreach 的条件? $numbers = array(5, 19, 8, 35, 50); $iterator = new
PHP 定义了两个 SPL无效键的异常(exception)情况: OutOfRangeException: Exception thrown when an illegal index was re
我有以下代码: @SuppressWarnings("resource") public void changeSkin(String name){ try { URL uui
这是我实现埃拉托斯特尼筛法的函数, void solve() throws IOException { int n = 200000; ArrayList primes = new A
这个问题已经有答案了: Array index out of bounds when getting arguments (4 个回答) 已关闭 7 年前。 我正在处理一项作业,但不允许我使用扫描仪。
我不是专家,不知道为什么会越界 public class ParticipantIndexComparator implements Comparator { final List parti
对于我的 CompSci 类(class),我们正在为我们的聊天机器人项目制作一个 Would You Rather? 功能。 String.split() 方法对此很有效,但如果我们不用它也能做到,
我是一名优秀的程序员,十分优秀!