【问题标题】:while loop. sum and product of user inputted numberswhile 循环。用户输入数字的总和和乘积
【发布时间】:2014-10-16 09:03:27
【问题描述】:

构建一个程序,该程序将使用 while...循环结构生成 20 个输入数字的和和乘积。

条件:

  • 用户只需输入 20 个数字
  • 并得到所有数字的总和和乘积。

我现在的代码是这样的

import java.util.Scanner;

public class Case3 {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        int[] list = new int[20];
        int sum = 0;
        int product = 0;
        int x = 0;
        int number;

        System.out.print("Add number " + (x + 1) + ": ");
        number = input.nextInt();

        while (x <= list.length) {
             list[x] = number;
             x++;
             System.out.print("Add number " + (x + 1) + ": ");
             number = input.nextInt();
        }

        for (int i = 0; i < x; i++) {
             sum += list[i];
             product *=list[i];
        }

        System.out.println("The sum of all values are: " + sum);
        System.out.println("The product of all values are: " + product);
      }

}  

--------配置:--------

加号1:1

加数字2:2

添加数字 3: 3

加数字 4: 4

添加数字 5:5

加数字 6:6

加数字 7: 7

加数字 8: 8

加数字 9: 9

加号 10:10

加号 11:11

加号 12:12

添加数字 13: 13

添加数字 14: 14

添加数字 15: 15

加号 16:16

添加数字 17: 17

添加数字 18: 18

加号 19:19

加号 20:20

加号 21:21

线程“主”java.lang.ArrayIndexOutOfBoundsException 中的异常: 20 在 Case3.main(Case3.java:16)

过程完成。

【问题讨论】:

  • 您刚刚发现当您尝试将 21 个数字放入大小为 20 的数组中时会发生什么

标签: java arrays loops


【解决方案1】:

替换

while (x <= list.length) {

while (x < list.length) {

这是因为最后一次迭代将填充数组中的 20 多个元素。

您还应该将 product 初始化为 1 而不是 0。

【讨论】:

    【解决方案2】:

    x++ 有问题,所以修改它现在可以工作了..

    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        int[] list = new int[19];
        int sum = 0;
        int product = 1;
        int x = 0;
        int number;
        System.out.print("Add number " + (x + 1) + ": ");
        number = input.nextInt();        
        while (x <list.length) {
             list[x] = number;   
             x++;//1,2,3
             System.out.print("Add number " + (x + 1) + ": ");
             number = input.nextInt();             
        }
        for (int i = 0; i < list.length; i++) {
             sum += list[i];
             product *=list[i];
        }
        System.out.println("The sum of all values are: " + sum);
        System.out.println("The product of all values are: " + product);      
    }
    

    【讨论】:

      【解决方案3】:

      while循环前的这两行不是必须的。

      System.out.print("Add number " + (x + 1) + ": ");
      number = input.nextInt();
      

      虽然必须是循环,但

       while (x < list.length) {
           System.out.print("Add number " + (x + 1) + ": ");
           list[x] = number;
           x++;
           number = input.nextInt();
       } 
      

      当您处理产品时,产品应该初始化为 1(而不是 0)。

      【讨论】:

        【解决方案4】:

        我假设你在问 “线程“主”java.lang.ArrayIndexOutOfBoundsException 中的异常” x 如果为 20,则为 int 数组大小,但其索引从 0 到 19。 使用此停止条件x &lt;= list.length x=0 到 20 有效地尝试初始化不存在的 x[20]。因此更改为 'x

        product = 1;
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2016-02-01
          • 1970-01-01
          • 1970-01-01
          • 2021-09-14
          • 2020-07-07
          • 2014-01-03
          • 2022-12-12
          相关资源
          最近更新 更多