当前位置: 首页 > 知识库问答 >
问题:

当总和超过100时,如何让此代码停止输入,并且仍然执行总和和平均值?

梁丘书
2023-03-14

我已经做了几个小时了!!!!赋值的更新指出,当用户输入的值超过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!");
    }
}

}

如果我在计数器后输入,用户在第一次输入后收到此消息。。。我做错了什么!!!!!!

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

中断的问题是“JOptionPane.showMessageDialog(null,“您的数字之和大于100!”)一段代码在中断后立即放置时导致了一些输出问题。现在的问题是总和的平均值

下面是代码:

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

    }
}

共有2个答案

宗政坚白
2023-03-14

将循环更改为

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

直到循环停止后,才需要计算平均值。

凤经国
2023-03-14

在你的中使用down语句,而循环带有if条件来检查主题

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

 类似资料:
  • 问题内容: 我已经在这里呆了几个小时了!分配的更新表明,当用户的值超过100时,我们需要停止用户输入。如果不重写整个内容,如何“循环”进行输入?我知道代码很繁琐,但这是分配的要求。我认为我的大脑只是java汤!任何帮助都是极好的 :) } 如果我在counter ++之后输入此消息,则用户在第一次输入后会收到此消息…我在做什么错!!!! 中断的问题是 “ JOptionPane.showMessa

  • 我有以下任务: > 编写一个程序,要求用户输入一个数字。 如果数字小于100,则要求用户输入另一个数字并将两者求和。 继续要求用户输入数字,直到所有输入数字的总和至少为100。 如果用户输入的第一个数字大于或等于100,请打印消息“此数字已超过100”,不要要求用户输入任何其他数字。 我可以打印用户输入的数字总和,但我就是无法停止。即使我使用了,它也会在100号之后中断。

  • 我尝试使用ireport对BigDecimal值进行sum和avg算术运算。我做的时候总会有误差。 所有类型都在 ireport 中定义为 BigDecimal 。 -分离,此示例中的变量工作正常,但我不能对变量进行算术运算。 总和与平均值如何键入BigDecimal? 在我的示例中,这不是工作: 我尝试在ireport中使用Java sintax,但是不起作用。 我使用的是3.0.0版iRepo

  • 问题内容: 我正在尝试从数据集中返回总计/平均值行,其中包含某些字段的SUM和其他字段的AVG。 我可以通过以下方式在SQL中执行此操作: 我将其转换为SQLAlchemy的尝试如下: 但这是错误的: 问题答案: 您应该使用类似: 您不能在这里使用,因为SqlAlchemy试图找到一个将函数结果放入的字段,但是它失败了。

  • 问题内容: 我正在使用SQL Server 2012,并且正在尝试执行以下操作: 因此,换句话说,我想从表中选择具有指定的截止日期和日期的随机行,并在里程总和等于或大于该数字时停止:3250 问题答案: 由于您使用的是SQL Server 2012,因此这是一种无需循环的简单方法。 SQLfiddle演示-多次单击“运行SQL”以查看随机结果。

  • 问题内容: 我在添加数组的所有元素以及将它们取平均值时遇到了问题。我该怎么做,并用我现在拥有的代码实现它?这些元素应该定义如下。 问题答案: var sum = 0; for( var i = 0; i < elmt.length; i++ ){ sum += parseInt( elmt[i], 10 ); //don’t forget to add the base } 只需遍历数组,因为您的