当总和超过100时,我怎样才能得到这个代码来停止输入,并仍然执行总和和平均值? [英] How do I get this code to stop input when the sum exceeds 100 and still preform the sum and average?

查看:194
本文介绍了当总和超过100时,我怎样才能得到这个代码来停止输入,并仍然执行总和和平均值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经在这个几个小时了!更新的作业指出,当数值超过100时,我们需要停止用户输入。在不重写整个事情的情况下,如何循环这个?我知道代码是沉重的方法,但它是一个任务的要求。我想我的大脑只是java汤!任何帮助将是真棒:)

pre $ code public static void main(String [] args){
// Use Main用于收集输入的方法
float input = 1;
//声明变量
float theSum = 0;
//声明变量的平均值
float average = 0;
//声明用于计算用户输入数量的变量
int counter = 0;
/ *初始化while循环,输入0作为标记值
*退出循环* /
while(input!= 0){
//使用JOptionPane方法接受来自用户的输入
input = Float.parseFloat(
JOptionPane.showInputDialog(
null,请输入一个数字,输入0退出:));
//调用sum方法并将输入和求和传递给sum方法
theSum =(sum(input,theSum));
//调用avg方法,并将summation和counter传给avg
average =(avg(theSum,counter));
//增加计数器变量
counter ++;
}
//调用显示方法并将总和,平均值和计数器变量传递给它
display(theSum,average,counter);

public static float sum(float num1,float sum){
//将用户输入的数字加到sum变量
sum + = num1;

//将sum变量的值作为新的求和变量
返回sum;

public static float avg(float num1,int num2){
//声明并初始化变量的平均值
float average = 0;

//计算平均值
average = num1 / num2;

//平均值
的平均回报值;

public static void display(float sum,float average,int counter){

/ *我从变量计数器中减去1,以便不包含标记值
*为0,用户必须输入以退出输入循环的总数* /

//显示用户的计数,总和和平均值
JOptionPane。 showMessageDialog(null,Count =+(counter-1)+,Sum =+ sum +,Average =+ average);

//测试总和是否大于100,如果是,则显示警告给用户
if(sum> 100){
JOptionPane.showMessageDialog(null,你的数字总和大于100!);













如果我在计数器之后输入这个值++用户在第一次输入之后得到这个消息...我做了什么错误!!!!!!

  if(theSum> 100)
break;
JOptionPane.showMessageDialog(null,你的数字总和大于100!);

中断的问题在于JOptionPane.showMessageDialog(null,你的数字大于100!);段的代码是在中断后立即产生一些输出问题。现在麻烦的是,总和> 100的平均值是输出总和,而不是平均值。它适用于总和< = 100 ... WTH?!?! :)

现在编码:

  public static void main (String [] args){
//使用Main方法收集输入
float input = 1;
//声明变量
float theSum = 0;
//声明变量的平均值
float average = 0;
//声明用于计算用户输入数量的变量
int counter = 0;
/ *初始化while循环,输入0作为标记值
*退出循环* /
while(input!= 0){
//使用JOptionPane方法来接受来自用户的输入
input = Float.parseFloat(
JOptionPane.showInputDialog(
null,请输入一个数字,输入0退出:));
//调用sum方法并将输入和求和传递给sum方法
theSum =(sum(input,theSum));
//调用avg方法,并将summation和counter传给avg
average =(avg(theSum,counter));
//增加计数器变量
counter ++;

if(theSum> 100)
break;
}
//调用显示方法并将总和,平均值和计数器变量传递给它
display(theSum,average,counter);

public static float sum(float num1,float sum){
//将用户输入的数字加到sum变量
sum + = num1;
//将sum变量的值作为新的求和变量
返回sum;

public static float avg(float num1,int num2){
//声明并初始化变量的平均值
float average = 0;
//计算平均值
average = num1 / num2;
//平均值
的平均返回值;

public static void display(float sum,float average,int counter){

/ *我从变量计数器中减去1,以便不包含标记值
*为0,用户必须输入以退出输入循环的总数* /

//显示用户的计数,总数和平均值
if( (计数=+(计数器)+,和=+和+,平均=+平均值);
if(sum <= 100)
JOptionPane.showMessageDialog(null,Count =+(counter-1)+,Sum =+ sum +,Average =+ average);


$ b $ / code $ / $ p

解决方案

中使用 break 语句,循环中使用如果条件来检查
thesum> 100 。希望这个提示能够帮助你获得理想的行为。因为你还需要计算平均值,所以把break逻辑post为avg:

pre $ while(input!= 0){
//使用JOptionPane方法接受来自用户的输入
input = Float.parseFloat(
JOptionPane.showInputDialog(
null,请输入数字,输入0退出:) );
//调用sum方法并将输入和求和传递给sum方法
theSum =(sum(input,theSum));
//调用avg方法,并将summation和counter传给avg
average =(avg(theSum,counter));
//增加计数器变量
counter ++;
if(theSum> 100)
break;
}
//调用显示方法并将总和,平均值和计数器变量传递给它
display(theSum,average,counter);


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

    }
}

解决方案

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

这篇关于当总和超过100时,我怎样才能得到这个代码来停止输入,并仍然执行总和和平均值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆