Multiplying input values

I am currently attempting a challenge question in one of my homeworks. For now I have written a code that adds all input values until [0] is inputted. The code goes like this:

do {
              results = scanner.nextInt();
              value += results;
} while (results != 0);

However, after attempting to write a code for multiplication, I keep getting my output as 0. I realized that one of the issues was due to the fact that I had previously set my variable value to ```= 0````. Therefore, I created a new variable with a set value of equal to 1. Because any number multiplied to one, is just that number. With many attempts I wrote this code:

 int product = 1;
                        do {
                            results = scanner.nextInt();
                            product *= results;
                        } while (results != 0);

I don' understand why this code is not working. My am multiplying 1 to the values inputted into the loop, so I don't get how the output is zero. Can somebody please help me?



Read more here: https://stackoverflow.com/questions/66305392/multiplying-input-values

Content Attribution

This content was originally published by Coder1235 at Recent Questions - Stack Overflow, and is syndicated here via their RSS feed. You can read the original post over there.

%d bloggers like this: