0

Hi i came up with the following code for my Assignment.It works.What i dont understand was the following code, the rest of the code i did myself.

if ( value == 1 )
    smallest = randomNum;

Why do i need to assign value to 1?
Rest of my codes

System.out.println("How many integers shall we compare? (Enter a positive integer):");
            ii = input.nextInt();

            for (value = 1;value<=ii; value++)   
            {
                System.out.print("Enter value " + value + ":");
                randomNum = input.nextInt();  

            if ( value == 1 )
                smallest = randomNum;


            else if (randomNum < smallest)
                smallest = randomNum;           

            }
            System.out.println("The smallest number entered was: " + smallest);
            }

}

.

2
Contributors
2
Replies
3
Views
4 Years
Discussion Span
Last Post by Valiantangel
1

First, '==' operator is not an assignment operator. It is comparison. Assignment operator is '='.

Second, the if statement is used to set the first value as your smallest value since it has nothing to compare to, it will always be the smallest value in your random number.

Have fun! = )

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.