How do I get this code to stop input when the sum exceeds 100 and still preform the sum and average?

北城余情 提交于 2019-12-29 02:01:09

问题


I have been at this for hours!!!! An update to the assignment states that we need to stop the user input when their values exceed 100. Without rewriting the whole thing, how do I "loop" this in? I know the code is method heavy but it was a requirement for the assignment. I think my brain is just java-soup! Any help would be AWESOME :)

public static void main(String[] args) {
    //Use Main Method for gathering input
    float input = 1;
    // Declare variable for sum
    float theSum = 0;
    // Declare variable for average
    float average = 0;
    // Declare variable for counting the number of user inputs
    int counter = 0;
    /* Initialize the while loop using an input of 0 as a sentinel value
     * to exit the loop*/
    while (input != 0) {
        // Use JOptionPane method to accept input from user
        input = Float.parseFloat(
                JOptionPane.showInputDialog(
                null, "Please enter a number.  Enter 0 to quit: "));
        // Invoke sum method and pass input and summation to sum method
        theSum = (sum(input, theSum));
        // Invoke avg method and pass summation and counter to avg
        average = (avg(theSum, counter));
        // Increment the counter variable
        counter++;
    }
    // Invoke display method and pass summation, average, and counter variables to it
    display(theSum, average, counter);
}
public static float sum(float num1, float sum) {
    //Add the user's input number to the sum variable
    sum += num1;

    //Return value of sum variable as new summation variable
    return sum;
}
public static float avg(float num1, int num2) {
    //Declare and initialize variable for average
    float average = 0;

    //Calculate average
    average = num1 / num2;

    //Return value of average variable
    return average;
}
public static void display(float sum, float average, int counter) {

    /* I am subtracting 1 from variable counter so as not to include the sentinel value
     * of 0 that the user had to enter to exit the input loop in the overall count*/

    // Display the count, sum, and average to the user
    JOptionPane.showMessageDialog(null, "Count = " + (counter - 1) + ", Sum = " + sum + ", Average = " + average);

    // Test to see if sum is greater than 100 and if so, display alert to user
    if (sum > 100) {
        JOptionPane.showMessageDialog(null, "The sum of your numbers is greater than 100!");
    }
}

}

if I enter this after counter++ The user gets this message after the 1st input...WHAT AM I DOING WRONG!!!!!!

if (theSum > 100)
            break;
                JOptionPane.showMessageDialog(null, "The sum of your numbers is greater than 100!");

The problem with the break was that the "JOptionPane.showMessageDialog(null, "The sum of your numbers is greater than 100!");" piece of code was causing some out put issues when placed immediately after the break. Now the trouble is that the average for the sum>100 is outputting the sum, not average. It works fine for the sum<=100...WTH?!?! :)

Heres the code now:

public static void main(String[] args) {
    //Use Main Method for gathering input
    float input = 1;
    // Declare variable for sum
    float theSum = 0;
    // Declare variable for average
    float average = 0;
    // Declare variable for counting the number of user inputs
    int counter = 0;
    /* Initialize the while loop using an input of 0 as a sentinel value
     * to exit the loop*/
    while (input != 0) {
        // Use JOptionPane method to accept input from user
        input = Float.parseFloat(
                JOptionPane.showInputDialog(
                null, "Please enter a number.  Enter 0 to quit: "));
        // Invoke sum method and pass input and summation to sum method
        theSum = (sum(input, theSum));
        // Invoke avg method and pass summation and counter to avg
        average = (avg(theSum, counter));
        // Increment the counter variable
        counter++;

        if (theSum > 100)
            break;                                
    }
    // Invoke display method and pass summation, average, and counter variables to it
    display(theSum, average, counter);
}
public static float sum(float num1, float sum) {
    //Add the user's input number to the sum variable
    sum += num1;        
    //Return value of sum variable as new summation variable
    return sum;
}
public static float avg(float num1, int num2) {
    //Declare and initialize variable for average
    float average = 0;
    //Calculate average
    average = num1 / num2;
    //Return value of average variable
    return average;
}
public static void display(float sum, float average, int counter) {

    /* I am subtracting 1 from variable counter so as not to include the sentinel value
     * of 0 that the user had to enter to exit the input loop in the overall count*/

    // Display the count, sum, and average to the user
    if (sum >100)
    JOptionPane.showMessageDialog(null, "Count = " + (counter) + ", Sum = " + sum + ", Average = " + average);
    if (sum <= 100)
    JOptionPane.showMessageDialog(null, "Count = " + (counter-1) + ", Sum = " + sum + ", Average = " + average);

    }
}

回答1:


Use a break statement in your while loop with a if condition to check the thesum > 100. Hope this much of hint will help you to get the desired behavior. As you need to calculate the average also, so put the break logic post avg:

     while (input != 0) {
    // Use JOptionPane method to accept input from user
    input = Float.parseFloat(
            JOptionPane.showInputDialog(
            null, "Please enter a number.  Enter 0 to quit: "));
    // Invoke sum method and pass input and summation to sum method
    theSum = (sum(input, theSum));
    // Invoke avg method and pass summation and counter to avg
    average = (avg(theSum, counter));
    // Increment the counter variable
    counter++;
    if (theSum > 100)
      break;
}
// Invoke display method and pass summation, average, and counter variables to it
display(theSum, average, counter);



回答2:


Change the loop to

do { ... } while (input != 0 && sum < 100);

You don't need to calculate the average until after the loop stops.



来源:https://stackoverflow.com/questions/18797170/how-do-i-get-this-code-to-stop-input-when-the-sum-exceeds-100-and-still-preform

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!