Share |

Find the Lowest Number from user inputs?

ok i need to know how you find the lowest value the user entered, and there can be unlimited inputs. so far i have the code below, but i dont know how to make it say the animal that has the least amount left in the wild and this number (as you can see from the ???? below in the code). i know you need to use if statements and somethinglike if (newcount<oldcount) {oldcount=newcount}. i tried this but it didnt work. need help thanks.

String anstext = ""; //
String ans1; //
int ans = 0; //

anstext = JOptionPane.showInputDialog("Name an animal?");

while (!anstext.equals("DODO"))
{
ans1 = JOptionPane.showInputDialog("How many are left in the wild?");
ans = Integer.parseInt(ans1);
anstext = JOptionPane.showInputDialog("Name an animal?");
}

JOptionPane.showMessageDialog(null, "The most endangered animal is the" + ??????)

JOptionPane.showMessageDialog(null, "There are only" + ?? "left in the wild")
SPB can u please put what you said into my code and reply thanks.

  1. SPB
    October 13th, 2010 at 00:03 | #1

    You need to keep track of the animal with the lowest number left. So, you need two more variables, lowestLeftNum and lowestLeftName.

    Before the while loop, ask your question. Set lowestLeftNum to ans and lowestLeftName to anstext. Then do your while loop. In the while loop say

    if (ans < lowestLeftNum)
    {
    lowestLeftNum = ans;
    lowestLeftName = anstext;
    }

    This will keep track of the one with the lowest number. Then, use these two variables in your output message boxes at the end.

  1. No trackbacks yet.

CommentLuv badge