[英]My loop in java is not working. Can anyone see why?
I have a lab in programming due soon. 我即将进行编程实验。 My program stops after asking for inputs after it asks for one.
我的程序在请求输入后要求输入停止。 I cannot see what I did wrong.
我看不到我做错了什么。 Can anyone see the problem?
谁能看到这个问题?
public static double dReadInputs(int numberOfInputs) //this accepts and fills an array with grade inputs
{
int t = 0;
double[] inputs = new double[numberOfInputs];
while (t < numberOfInputs)
{
JOptionPane.showInputDialog("Please enter 10 grade values one at a time: ");
inputs[10] = Integer.parseInt(in.nextLine());
t++;
}
return inputs[10];
The index 10 in inputs[10]
throws an exception because it might be greater than the size of the inputs
variable. inputs[10]
的索引10引发异常,因为它可能大于inputs
变量的大小。
This might work in your case: 这可能适用于您的情况:
inputs[t] = Integer.parseInt(in.nextLine());
public static double[] dReadInputs(int numberOfInputs) //this accepts and fills an array with grade inputs
{
int t = 0;
double[] inputs = new double[numberOfInputs];
while (t < numberOfInputs)
{
JOptionPane.showInputDialog("Please enter "+numberOfInputs+"grade values one at a time: ");
inputs[t] = Integer.parseInt(in.nextLine());
t++;
}
return inputs;
}
Basically, you want to get all the inputs so you want to return a double array instead of just a single double. 基本上,您希望获得所有输入,因此要返回一个double数组,而不是一个double。 Also, make sure you use a counter (or even better yet create a for loop) so you update the respective double in the double array.
另外,请确保使用计数器(甚至更好的是创建一个for循环),以便更新double数组中的相应double。
What's happening is that you're always writing to the 11th item in the array when you only have an array of size 10, so it's giving an ArrayOutOfBoundsException. 发生的情况是,只有大小为10的数组时,您总是在写入数组中的第11个项目,因此它给出了ArrayOutOfBoundsException。
Based on my understanding from your question description. 根据您对问题描述的理解。 You want the JOptionPane to show 10 times and each time take input and assign it to the array.
您希望JOptionPane显示10次,并且每次接受输入并将其分配给数组。 See below:
见下文:
public static double[] dReadInputs(int numberOfInputs)
{
int t = 0;
double[] inputs = new double[numberOfInputs];
while (t < numberOfInputs)
{
String in = JOptionPane.showInputDialog("Please enter value for grade " + (t + 1) + " : ");
inputs[t] = Double.parseDouble(in);
t++;
}
return inputs;
}
You can also protect your code against possible NullPointerException which can happen when user do not provide input and close the input dialog using Cancel or Close buttons. 您还可以保护代码免受可能的NullPointerException的影响 ,该异常可能在用户不提供输入并使用“ 取消”或“ 关闭”按钮关闭输入对话框时发生。 Solution:
解:
if (in != null) {
//inform user to enter valid +ve integer/double
//you might wanna continue back to loop without incrementing t
}
You can protect the code against NumberFormatException by making sure that the user always inputs valid number and nothing else. 您可以通过确保用户始终输入有效数字而不输入其他任何内容来保护代码免受NumberFormatException的侵害。 Even empty input dialogue would cause exception: Solution:
即使是空的输入对话框也会导致异常:解决方案:
try {
inputs[t] = Double.parseDouble(in);
} catch(NumberFormatException nfe) {
//inform user to input valid +ve integer/double
//you might wanna continue back to loop without incrementing t
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.