Hi I want to know what's the problem with my code. I'm just a beginner. I'm trying to display the smallest number, but everytime I run the program, the output is always equal to zero. However if I use this code to find the largest number, wherein the boolean condition in if statement is (number>=largest), it works. But if I change it to (number<=smallest) to find the smallest number, it does not work.
Here's my code:

import javax.swing.*;
public class HighestNum {
public static void main (String args[])
{
int value = 0,number=0,smallest = 0;
while (value<10)
{
value++;
number = Integer.parseInt(JOptionPane.showInputDialog("Enter a number "+value+":"));
}
if (number <= smallest)
smallest=number;
else
smallest = smallest;
String msg="The smallest number is :"+smallest;
JOptionPane.showMessageDialog(null,msg);
}
}

Recommended Answers

All 2 Replies

Your initial value for smallest is 0 - think that through. What result do you expect with that starting value?
If you still don't get it post again and I'll give you a more explicit hint.
ps: Please post all code in code tags in future.

I already figured it out! By the way, I still want to thank you! :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.