当前位置: 首页 > 面试题库 >

当总和超过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!");
    }
}

}

如果我在counter ++之后输入此消息,则用户在第一次输入后会收到此消息…我在做什么错!!!!

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

中断的问题是 “ JOptionPane.showMessageDialog(null,”您的数字总和大于100!“);”
在中断后立即放置一段代码会导致一些输出问题。现在的麻烦是,> 100的和的平均值正在输出和,而不是平均值。对于总和<= 100 … WTH?!?!:)

现在继承代码:

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

    }
}

问题答案:

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


 类似资料:
  • 我已经做了几个小时了!!!!赋值的更新指出,当用户输入的值超过100时,我们需要停止用户输入。不重写整个事情,我如何“循环”这个?我知道代码方法很重,但这是作业的要求。我想我的大脑只是java汤!任何帮助将是真棒:) } 如果我在计数器后输入,用户在第一次输入后收到此消息。。。我做错了什么!!!!!! 中断的问题是“JOptionPane.showMessageDialog(null,“您的数字之

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

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

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

  • 本文向大家介绍如何在MongoDB中汇总总和以获得总数?,包括了如何在MongoDB中汇总总和以获得总数?的使用技巧和注意事项,需要的朋友参考一下 要在MongoDB中汇总总和以获取总计数,可以使用$sum运算符。要了解上述概念,让我们使用文档创建一个集合- 在method的帮助下显示集合中的所有文档。查询如下- 以下是输出- 这是获取总数的查询。 情况1-查询如下- 以下是输出- 这是在Mong

  • Flink(批处理/流式处理)中是否有方法同时计算字段的平均值和总和?使用聚合方法,我可以计算groupBy结果中字段的和,但如何同时计算平均值呢?下面的示例代码。