TreeMoney
06-29-2006, 06:15 AM
I'm trying to study for an exam, and I've written this program which finds the 2 smallest numbers out of a list. It works as long as the lowest # isn't first., I can't figure it out. Any ideas?? Thanks in advance.
public class Smallest
{
public static void main(String[] args)
{
int numNums, number, count, small, smallest;
numNums = IO.readInt();
if (numNums<0)
{
IO.reportBadInput();
}
int temp = IO.readInt();
small= temp;
smallest= temp;
count = 1;
while (count < numNums)
{
number = IO.readInt();
count++;
if(number<smallest)
{
temp = smallest;
small = temp;
smallest = number;
}
else {
if (number<small)
{
small = number;
}
}
}
IO.printInt(small);
IO.printInt(smallest);
}
}
public class Smallest
{
public static void main(String[] args)
{
int numNums, number, count, small, smallest;
numNums = IO.readInt();
if (numNums<0)
{
IO.reportBadInput();
}
int temp = IO.readInt();
small= temp;
smallest= temp;
count = 1;
while (count < numNums)
{
number = IO.readInt();
count++;
if(number<smallest)
{
temp = smallest;
small = temp;
smallest = number;
}
else {
if (number<small)
{
small = number;
}
}
}
IO.printInt(small);
IO.printInt(smallest);
}
}