【问题标题】:How do I get this code to stop input when the sum exceeds 100 and still preform the sum and average?当总和超过 100 并且仍然执行总和和平均值时,如何让此代码停止输入?
【发布时间】:2013-09-18 18:46:15
【问题描述】:

我已经在这几个小时了!!!!对分配的更新指出,当用户输入的值超过 100 时,我们需要停止用户输入。在不重写整个内容的情况下,我如何“循环”它呢?我知道代码方法繁重,但这是分配的要求。我认为我的大脑只是 java 汤!任何帮助都会很棒:)

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!");
    }
}

}

如果我在 counter++ 之后输入此信息 用户在第一次输入后收到此消息...我做错了什么!!!!!!

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

中断的问题是 "JOptionPane.showMessageDialog(null, "The sum of your numbers is greater than 100!");" 这段代码导致了一些输出问题休息后立即放置。现在的问题是 sum>100 的平均值输出的是总和,而不是平均值。它适用于 sum

下面是代码:

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);

    }
}

【问题讨论】:

  • 在您希望以 if 为条件的两个语句周围添加花括号。此外,break 应该是 if 中的最后一件事,否则其他语句将无法访问。所以你想要这样的东西:if (theSum &gt; 100) { JOptionPane.yaddaYadda(); break; }
  • 您不需要每次在循环中重新计算平均值:您可以在之后进行。而且您真的不需要一种方法来将两个数字相加。
  • 作业的一部分是过度使用方法(我们这周学习了方法。你能告诉我吗:))我愿意接受有关清理总和的建议...

标签: java loops for-loop while-loop


【解决方案1】:

在带有if 条件的while 循环中使用break 语句来检查 thesum &gt; 100。希望这些提示将帮助您获得所需的行为。由于您还需要计算平均值,所以将 break 逻辑放在 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);

【讨论】:

  • 我试过了。你能告诉我我做错了什么吗? ***阅读对 OP 的编辑
  • 等一下。你输入了“ if (theSum > 100) break; ”,输出到底是什么?
  • 问题是我试图在休息后立即显示一个对话框......我现在解决了这个问题我只需要弄清楚为什么平均值不适用于 sum
【解决方案2】:

将循环改为

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

在循环停止之前,您不需要计算平均值。

【讨论】:

    猜你喜欢
    • 2016-06-30
    • 2019-09-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多