如何将用户输入(来自对话框)转换为数组。

huangapple go评论95阅读模式
英文:

How to convert User input (from dialog boxes) into arrays

问题

我真的需要在我的程序中得到帮助。我正在尝试使用Java、数组和对话框制作一个累积成绩计算器。由于某种原因,我的数组不会打印出用户输入的内容,这影响了我的计算。我该如何为它赋予正确的值?

public static int getGrades()
{
    // 第一个数组 - 长度
    int[] arNumber = null;
    int number;
    String str;
    
    // 第二个数组 - 元素
    int numbers;
    int[] arNumbers = null;
    int total = 0;
    int gradeSum = 0;
    String str2;
    
    String message = "你要输入多少个成绩?";
    str = JOptionPane.showInputDialog(message);
    number = Integer.parseInt(str);
    arNumber = new int[number];
    
    for(int index = 0; index < arNumber.length; index++)
    {
        String message2 = "请输入你的成绩";
        str2 = JOptionPane.showInputDialog(message2);
        numbers = Integer.parseInt(str2);
        arNumbers = new int[numbers];
        
    }
    for (int element : arNumbers)
    {
        // 将数组打印到控制台
        System.out.println(element);
        
        // 添加所有元素
        gradeSum += element;
        
        // 将成绩打印到控制台
        System.out.println(gradeSum);
    }
   
    total = gradeSum / arNumber.length;
    
    return total;
    
}
英文:

I really need help with my program. I'm trying to make a cumulative grade calculator in java, using arrays and dialog boxes. For some reason, my arrays won't print out the input made by the user which is throwing off my calculations. How can I give it the correct value?

    {
        // First array - Length
        int[] arNumber = null;
        int number;
        String str;
        
        // Second array - Elements
        int numbers;
        int[] arNumbers = null;
        int total = 0;
        int gradeSum = 0;
        String str2;
        
        String message = &quot;How many grades will you input in this class?&quot;;
        str = JOptionPane.showInputDialog(message);
        number = Integer.parseInt(str);
        arNumber = new int[number];
        
        for(int index = 0; index &lt; arNumber.length; index++)
        {
            String message2 = &quot;Insert your grade&quot;;
            str2 = JOptionPane.showInputDialog(message2);
            numbers = Integer.parseInt(str2);
            arNumbers = new int[numbers];
            
        }
        for (int element : arNumbers)
        {
            // Print array onto console
            System.out.println(element);
            
            // Add all elements 
            gradeSum += element;
            
            // Print grade onto console
            System.out.println(gradeSum);
        }
       
        total = gradeSum / arNumber.length;
        
        return total;
        
    }```

</details>


# 答案1
**得分**: 0

看起来您不需要使用两个数组。带有 `arNumbers = new int[numbers];` 的那一行意味着在每次循环迭代中都会将 `arNumbers` 重新初始化为一个新数组。尝试使用 `arNumber[index] = numbers;` 将用户输入的值赋值给 `arNumber` 数组,并且不再使用 `arNumbers`。然后在第二个循环中遍历 `arNumber` 数组。

另外要注意,`total` 被声明为 `int`,这会截断您的浮点值,我猜这可能不是您想要的。祝您好运!

<details>
<summary>英文:</summary>

It looks like you don&#39;t need to use two arrays. The line with `arNumbers = new int[numbers];` means that `arNumbers` is getting reinitialized as a new array for every iteration of that loop. Try using `arNumber[index] = numbers;` to assign the user-entered value into the `arNumber` array and do away with `arNumbers`. Then loop over `arNumber` in the second loop.

Also note that `total` is declared as an `int` which will truncate your floating point value, which I&#39;m guessing you don&#39;t want. Good luck!

</details>



huangapple
  • 本文由 发表于 2020年5月2日 13:02:43
  • 转载请务必保留本文链接:https://go.coder-hub.com/61554706.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定