简体   繁体   中英

while loop. sum and product of user inputted numbers

Build a program that will generate the sum and product of 20 input numbers using the while…loop structure.

Conditions:

  • user need to input only 20 numbers
  • and get the sum and product of all the numbers.

My code right now is this

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);
      }

}  

--------------------Configuration: --------------------

Add number 1: 1

Add number 2: 2

Add number 3: 3

Add number 4: 4

Add number 5: 5

Add number 6: 6

Add number 7: 7

Add number 8: 8

Add number 9: 9

Add number 10: 10

Add number 11: 11

Add number 12: 12

Add number 13: 13

Add number 14: 14

Add number 15: 15

Add number 16: 16

Add number 17: 17

Add number 18: 18

Add number 19: 19

Add number 20: 20

Add number 21: 21

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 20 at Case3.main(Case3.java:16)

Process completed.

Replace

while (x <= list.length) {

with

while (x < list.length) {

This is because the last iteration will fill more than 20 elements in the array.

Also you should initialize product to 1 instead of 0.

problem is with x++ so modified it now it works..

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);      
}

This two lines before the while loop is not required.

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

While loop has to be,

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

And the product should be initiated to 1 (NOT to 0), when you are dealing with products.

我假设您在询问“线程“main”中的异常 java.lang.ArrayIndexOutOfBoundsException” x 是一个 int 数组大小,如果 20 但它的索引从 0 到 19。使用此停止条件x <= list.length x=0 直到20 有效地尝试初始化不存在的 x[20]。因此更改为 'x < list.length' 以及

product = 1;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM