Question

The program I'm trying to write needs to input a user-defined number of Int values and produce the product of those values, and then determine all of the factors of that product. The values will be input through a Do-While Loop until the user enters a negative number, and then all the positive numbers will be multiplied together to form a product in the form of a Double. I'm fairly certain I can figure out how to get the factors of the product, but when it comes to finding the actual product I'm completely at a loss. Here is the code I've written so far, minus my failed attempts at producing a product.

public static void main(String[] args)
{
    Scanner in = new Scanner(System.in);
    int Value;
    double Product;

    do
    {
        System.out.print("Value: ");
        Value = in.nextInt();
    }
    while (Value>=0);
}

This is all I've been able to figure out, looping the Value prompt until a negative Int is entered. As far as getting the product I've tried If-Statements both inside and outside the loop, but either my code is wrong or my statements are wrong, or more likely I'm just trying the wrong method. I'm not asking anybody to write the code for me, but if somebody could point me in the right direction I would very much appreciate it.

What tips would you suggest to determine the product from an unknown number of user-difined values?

Was it helpful?

Solution

You should set product variable to be initially equal to 1 and multiply the value of product by the input number each time the user writes a number.

public static void main(String[] args)
{
    Scanner in = new Scanner(System.in);
    int value;
    double product=1;

    do
    {
        System.out.print("Value: ");
        value = in.nextInt();
        product *= value //product= product * value;
    }
    while (value>=0);

    System.out.println("Product is: "+product);
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top