java - For循环中的无限循环
问题描述
我试图更改我的程序以显示月份的名称。
private static void getValues(double [] array)
{
Scanner keyboard = new Scanner (System.in);
//get the values of the months
for (int i = 0 ; i < array.length ; i ++)
{
System.out.println("Enter the value for the month " + (i+1));
array[i] = keyboard.nextDouble();
if (array[i] < 0 )
{
System.out.println("The value SHOULD NOT BE a NEGATIVE number");
break;
}
}
}
}
我将代码更改为以下内容:
String [] month = {"January", "February","March",etc};
for ( int i = 0 ; i < array.length ; i ++)
{
for (int index = 0; month.length; index++)
{
System.out.println("The rainfall value of the " + month[index]);
array[i] = keyboard.nextDouble();
if (array[i] < 0 )
{
System.out.println("The value SHOULD NOT BE a NEGATIVE number");
break;
}
}
}
它运行了,但我创建了一个无限循环。我在哪里错过了它?十分感谢大家。
解决方案
您在第二个 for 循环中缺少检查。
for (int index = 0; month.length; index++)
条件应该index < month.length
不是month.length
。
推荐阅读
- reactjs - 传入新道具时重置反应选择下拉菜单
- python - 使用 CountVectorizer 从文件列表创建稀疏矩阵失败
- python - 有没有办法在元素前面添加数字
- python - 内存不足 - Keras tensorflow GPU - 梯度累积
- java - 如何在 Google Ads API 中查询最近停用的广告系列
- java - @PreAuthorize 带有允许角色列表的注释包装器
- vba - vbCFBitmap 变量未定义
- android-studio - Android Studio 的播放按钮未启用
- javascript - 仅自动滚动子组件,而不是父组件
- r - R中的字符串替换不正确