- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有一个问题,希望需要一个我似乎无法弄清楚的简单明显的解决方案。好的,我正在研究斐波那契数列:我想提示用户输入一个整数。然后它将运行将每个元素添加到 Arraylist 的序列。然后,我想使用计时器将数组列表中的每个元素显示到控制台 - 每秒显示序列中的每个元素 - 我用一个简短的方法来执行此操作。
输出没问题。当我运行该程序时,我收到 IndexOutOfBoundsException。我明白为什么会出现这个问题。我的计数超出了数组列表的大小。我想过只是乱搞 >、= 等 - 但没有解决问题。我想过用 while 循环来解决错误 - 但这不起作用。我的问题是:如何在不超出数组列表索引限制的情况下进行计数?
这是我的代码:
public class Sequence {
static int seconds = 0;
static Timer timer;
static ArrayList<Integer> fibList = new ArrayList<Integer>();
public static void main(String[] args) {
boolean noError = true;
Scanner sc = new Scanner(System.in);
//will store sum of evens
int tmp = 0;
//timer variables
int delay = 1000;
int period = 1000;
timer = new Timer();
//enter user input, try catch to eliminate invalid input
do{
try{
System.out.println("Please enter a number: ");
int input = Integer.parseInt(sc.nextLine());
//add to arraylist from user input
for (int i=0; i<=input; i++){
fibList.add(fib(i));
//sum even numbers
if(fib(i)%2 == 0){
tmp += fib(i);
}
}
}catch(Exception e){
System.out.println("Not a valid Number!");
}
noError = false;
}while(noError);
System.out.println(fibList);
System.out.println("Sum of Even Numbers is: "+tmp);
timer.scheduleAtFixedRate(new TimerTask() {
public void run() {
System.out.print(fibList.get(setInterval())+ ",");
}
}, delay, period);
}
//This is where I count up through my ArrayList and probably cause the Error
//counts up for every element through the array
public static final int setInterval() {
if (seconds >= fibList.size())
timer.cancel();
return seconds++;
}
//does fibonacci sequence
public static int fib(int n) {
if (n < 2) {
return n;
}
else {
return fib(n-1)+fib(n-2);
}
}
}
最佳答案
顺便说一句,
iluxa
的方法更好。逻辑清晰,避免困惑。
但是,如果您只是想修复测试条件,请将 if
更改为
if (seconds == fibList.size() - 1)
关于java - Java 中计时器的 IndexOutOfBoundsException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16429764/
这个问题已经有答案了: What causes a java.lang.ArrayIndexOutOfBoundsException and how do I prevent it? (25 个回答)
((fn [coll] (letfn [(add-item [acc coll idx] (conj acc (nth coll idx))
我不是 Java 初学者,但也不是专家,这就是我发布此文章以寻求帮助/解释的原因。我在互联网上查找了很多地方,但没有找到我正在寻找的答案。 public class Driver { public s
我不断得到 线程“main”中出现异常 java.lang.ArrayIndexOutOfBoundsException:MergeSorter.merge(MergeSorter.java:44)、
我做了一个小射击游戏..它工作正常但我也想实现如果火相交它们会消失。我有两个播放器子弹列表和计算机子弹列表......但是如果我有更多来自计算机或反向的子弹。这是我的循环 for (int
我试图从 JOptionPane 读取像 3+9-2*10/5 这样的数学表达式并得到它的结果——当然要考虑到操作顺序。我使用 String.split() 将字符串拆分为数字和操作数,并创建了一个寻
我在Grails中有以下代码: public LibraryItem createLibraryItemWithValues(ProjectItem projectItem) { def li
这个问题已经有答案了: What is a debugger and how can it help me diagnose problems? (3 个回答) 已关闭 7 年前。 我有一组以下类型的
请看下面的代码 DatabaseHandler.java public List getDetails(String name) { List details = new Ar
我应该让蛇形矩形沿着屏幕的一侧移动。然而它保持在原位,当我运行它时,我还收到 IndexOutOfBoundsException 错误。我相信这与我的 Render.java 文件中的 for 循环有
ArrayList beds = new ArrayList(49); public Patient getPatient(int bedNumber) { if (beds.get(bedN
我有 10 张卡片的数组。在“回合”的过程中,我向数组添加一张卡片,然后从数组中删除一张卡片(并不总是相同的)。该代码在前 6-7 回合中效果很好,然后抛出 IndexOutofBoundsExcep
我正在尝试创建一个评分系统并获得最佳分数,但我的方法生成了 IndexOutOfBoundsException,但我找不到超出数组列表范围的内容,有人可以帮助我吗? 代码: public stati
为什么 int row = 1; // multiArray[col].length = 6 while(multiArray[col][row] > 1 && row 1) { 关于java -
我制作了一款游戏,我们控制一艘宇宙飞船,可以发射激光摧毁来自顶部的小行星。但在游戏过程中,每当它抛出 IndexOutOfBoundsException 时,我的游戏就会卡住。我不知道为什么会这样。请
我正在尝试实现合并排序,但我不断收到 IndexOutOfBoundsException 并且我无法找出原因。 我已经调试了我的程序,并且在此函数中我从未使用无效索引访问数组。引发异常的行也与抛出异常
我无法弄清楚为什么我的代码出现 IndexOutOfBoundsException。我想知道是否有人可以成为我的额外眼睛来帮助我发现错误。我正在手动尝试这个,但我认为在浏览代码时我遗漏了一些东西。 代
为什么我会收到此错误? Caused by: java.lang.IndexOutOfBoundsException: Invalid index 1, size is 0 at java.util.
我不知道这是否是一个简单的问题,但我只是看不出问题是什么。我现在从 Google Play 中的应用收到了三份关于 points.get(++i) 处的 IndexOutOfBoundsExcepti
如何执行以下内容 String p = "abcd"; System.out.print(p.substring(4)); 不会导致java.lang.IndexOutOfBoundsExceptio
我是一名优秀的程序员,十分优秀!