Simple stats with input numbers program

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • powerej
    New Member
    • Sep 2007
    • 6

    #1

    Simple stats with input numbers program

    writing a program that asks how many numbers the users want to enter, then input an integer for the amount of numbers said, output: number of integers entered, sum of them, average of them, maximum value entered and min value entered. the min is where i am having a problem i can get everything else to print out but i am doing something wrong in my while loop can someone please help me.

    [CODE=java] import javax.swing.*; // gets option pane

    class Program3 {

    public static void main (String[] args) {

    String userInput;
    String userInput2;
    String output = "";
    int amountEntered;
    int integerEntered;
    int sum = 0;
    float average = 0;
    int maxNum;
    int minNum;
    int number;
    float numberOfInteger s = 0;

    // asking for input
    userInput = JOptionPane.sho wInputDialog("H ow many numbers would you like to enter?");

    if (userInput == null){
    JOptionPane.sho wMessageDialog( null, "Cancel was selected. Program will exit.",
    "Program3 (Edward Powers)", JOptionPane.ERR OR_MESSAGE);
    System.exit(0);
    }

    amountEntered = Integer.parseIn t(userInput);
    numberOfInteger s = amountEntered;

    if (amountEntered < 0) {
    JOptionPane.sho wMessageDialog( null, "You entered a negative number, so the program terminates.",
    "Program3 (Edward Powers)", JOptionPane.ERR OR_MESSAGE);
    System.exit(0);
    }

    if (amountEntered == 0) {
    JOptionPane.sho wMessageDialog( null, "You requested no numbers, so there \n" +
    "is no sum, average, minimum or maximum.",
    "Program3 (Edward Powers)", JOptionPane.ERR OR_MESSAGE);
    System.exit(0);
    }



    while(amountEnt ered > 0)
    {

    userInput2 = JOptionPane.sho wInputDialog("e nter an integer:");

    if (userInput2 == null)
    {
    JOptionPane.sho wMessageDialog( null, "Cancel was seleceted, program will exit.",
    "Program 3 (Edward Powers)", JOptionPane.ERR OR_MESSAGE );
    System.exit(0);
    }

    integerEntered = Integer.parseIn t(userInput2);
    number = integerEntered;
    maxNum = number;
    minNum = number;

    if (number > maxNum)
    {
    maxNum = number;
    }
    if (number < minNum)
    {
    minNum = number;
    }

    sum += integerEntered;
    average = sum / numberOfInteger s;

    amountEntered--;

    output = "Number of integers entered :" + (int)numberOfIn tegers + "\n" +
    "Sum of those integers is : " + sum + "\n" +
    "Average of those integers is : " + average + "\n" +
    "Maximum value of those integers : " + maxNum + "\n" +
    "Minimum value of those integers : " + minNum ;

    }




    JOptionPane.sho wMessageDialog( null,output,"Pr ogram3 (Edward Powers)",
    JOptionPane.PLA IN_MESSAGE);

    }
    }[/CODE]
    Last edited by Ganon11; Sep 29 '07, 07:58 PM. Reason: Please use the [CODE] tags provided.
  • powerej
    New Member
    • Sep 2007
    • 6

    #2
    my true problem is i know i can initialize my max and min values to like a million and i will get the correct min value but my teacher said this i weak programming and i cant think of a way to initialize these values with an arbitrary number

    Comment

    • Ganon11
      Recognized Expert Specialist
      • Oct 2006
      • 3651

      #3
      powerej - Please don't double post your question. There's no need for 2 threads addressing the same topic.

      Comment

      • Ganon11
        Recognized Expert Specialist
        • Oct 2006
        • 3651

        #4
        As to your question, think about the situation where the user has entered only 1 number. As of right now, what's the largest number? The only number the user has entered. And the smallest number? It also happens to be the only number the user has entered. Once you handle this simple case, your while loop will take care of the rest.

        Comment

        Working...