简体   繁体   English

我在Java中的循环无法正常工作。 谁能看到原因?

[英]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.

相关问题 谁能看到我程序中的无限循环在哪里? - Can anyone see where the infinite loop in my program is? 我不确定为什么我的键绑定不起作用。 [Java 键绑定] - I'm not sure why my keybinds are not working. [Java Keybinding] 我的交换方法不起作用。 谁能帮忙找出问题所在? - My Swap method is not working. Can anyone help figure out whats wrong? 任何人都可以解释为什么我的java代码组播不通过LAN工作? - Can anyone explain why my java code multicast is not working over LAN? 我的matchs()代码无法正常工作。 为什么不起作用? - My code with matches() is not working. Why is it not working? 我尝试在我的小型 android 项目中使用意向电子邮件,但它不起作用。 谁能帮我找出错误? - I tried to use intent email in my small android project but its not working. Can anyone help me to find the error? Java中的匿名内部类不起作用。 为什么? - Anonymous Inner Class in Java not working. Why? 谁能告诉我为什么在我的 evaulateExpression 中使用 pop() 不起作用? - Can anyone tell me why the use of pop() in my evaulateExpression is not working? Java Android - 为什么我的调试不能看到类 - Java Android - why can my debug not see into class Java-为什么我在JFrame中看不到我的列表? - Java - Why i can't see my list in JFrame?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM