简体   繁体   English

如何在do-while循环中找到平均值

[英]How to find the average in a do-while loop

I have to write a program that asks the user to enter an integer value. 我必须编写一个程序,要求用户输入一个整数值。 After each value, the user has to respond with a "y" or a "n" if he/she wants to continue with the program, and each number the user enters is stated as either odd or even. 在每个值之后,如果用户要继续执行程序,则用户必须以“ y”或“ n”作为响应,并且用户输入的每个数字都表示为奇数或偶数。

I have done this so far with a do-while loop, but I am confused on how to get the averages of the values the user enters. 到目前为止,我已经通过do-while循环完成了此操作,但是我对如何获取用户输入值的平均值感到困惑。 How would you get the average for all the numbers entered? 您将如何获得所有输入数字的平均值?

Here is my code so far: 到目前为止,这是我的代码:

import java.util.Scanner;
class ProgramTest {
    public static void main(String[] args) {
        String answer = "";

        do {

            int num, count = 0;
            Scanner scan = new Scanner(System. in );
            System.out.print("Enter any number : ");
            num = scan.nextInt();

            if ((num % 2) == 0) System.out.println(num + " is an even number.");
            else System.out.println(num + " is an odd number");
            System.out.println("do you want to continue?");
            answer = scan.next();
            count++;

        } while (answer.equals("y"));

    }
}

From the Question looks like following things need to handled, 从问题看来,以下事情需要处理,

  1. haven't add mechanism for addition into single variable. 尚未向单个变量添加添加机制。
  2. put all variable to out from do...while loop body... 将所有变量从do中移出... while循环体...
  3. created additional variable according to requirement. 根据需要创建其他变量。

see all this things covered by me with following code snippet. 通过以下代码片段查看我所涵盖的所有内容。

do something likewise, 做同样的事情,

String answer = "";
double sum = 0; // use for storing addition to all entered values..
int num, count = 0;
 Scanner scan = new Scanner(System.in);

        do {
            System.out.print("Enter any number : ");
            num = scan.nextInt(); // getting input from user through console
            sum = sum + num; // add every input number into sum-variable
            if ((num % 2) == 0) System.out.println(num + " is an even number.");
            else System.out.println(num + " is an odd number");
            System.out.println("do you want to continue?");
            answer = scan.next(); // ask for still want to repeat..
            count++;

        } while (answer.equals("y"));
System.out.println("Average is : " + sum + "/" + count + " = "+ (sum /count));

In order to calculate Average, you need 2 things: Sum of all numbers and Count of all numbers involved in the Average calculation. 为了计算平均值,您需要做两件事:平均值计算中涉及的所有数字的总和和所有数字的计数。

Your sum and count which involved in the Average calculation needs to be out of the do..while scope in order for them to be known at the calculation stage. 您的sumcount其参与平均值计算需要走出的do..while范围,以便让他们在计算阶段是已知的

I also took the liberty of fixing your code a little bit 我还自由地修复了您的代码

import java.util.Scanner;

class ProgramTest {

    public static void main(String[] args) {
        Scanner scan = new Scanner(System. in );
        int count = 0;
        int sum = 0;
        String answer = "";
        do {
            System.out.print("Enter any number : ");
            int num = scan.nextInt();

            boolean isEven = (num % 2 == 0);

            System.out.println(num + " is an " + (isEven ? "even" : "odd") + " number.");
            sum += num;

            System.out.println("do you want to continue?");
            answer = scan.next();
            count++;
        } while (answer.toLowerCase().equals("y"));

        System.out.println("Average: " + (sum/count));
    }
}

Change your code like this: 像这样更改代码:

  import java.util.Scanner;
    class ProgramTest {
        public static void main(String[] args) {
            String answer = "";
            int avr =0;
            int num, count = 0;
            do {

                Scanner scan = new Scanner(System. in );
                System.out.print("Enter any number : ");
                num = scan.nextInt();

                if ((num % 2) == 0) System.out.println(num + " is an even number.");
                else System.out.println(num + " is an odd number");
                System.out.println("do you want to continue?");
                avr += num;
                answer = scan.next();
                count++;

            } while (answer.equals("y"));
           avr = avr /count;
     System.out.println("The avreage of value is:" + avr );
        }
    }

avr is average. avr是平均水平。 that means when you input an integer. 这意味着当您输入整数时。 we add num and avr . 我们添加num和avr。 and when finish looping. 当完成循环时。 we divideto count. 我们划分为数。 like this: 像这样:

1-5-9-11 1-5-9-11

avr = 1+5+9+11; avr = 1 + 5 + 9 + 11;

count = 4; 计数= 4;

avr = avr/4; avr = avr / 4;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM