java 如何为这个方程编写一个java程序

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12765032/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 10:07:27  来源:igfitidea点击:

How to write a java program for this equation

javamath

提问by Sami Serbey

Please take a look at this topic : Can I calculate the average of these numbers?How can I write a program for this? In the right answer, he just wrote the equation without the java program and he used for example newValue without intialise the newValue. Here is the equation:

请看一下这个主题: 我可以计算这些数字的平均值吗?我该如何为此编写程序?在正确的答案中,他只是在没有 java 程序的情况下编写了等式,并且他使用了例如 newValue 而不初始化 newValue。这是等式:

int currentScore = (currentScore * currentCount + newValue) / currentCount;

回答by Yogendra Singh

Please find broken steps as below:

请找到以下损坏的步骤:

   private float calculateNewScore(){
        float currentScore = 6.1123
        int currentCount = 12;
        float newValue = 4.5;
        int newCount = currentCount+1;

        float newScore = (currentScore * currentCount + newValue) / newCount ;

    }

If you are looking for sample program, it can be something like this:

如果您正在寻找示例程序,它可以是这样的:

 public class CalculateScore {

  public static void main(String[] args) {
    float currentScore = 6.1123; //you can initialize with any desired value
    int currentCount = 12;//you can initialize with any desired value

    float newValue = 0;
    Scanner scanner = new Scanner( System.in );     

    do{
        System.out.println("Enter your new value. Enter 0 or negative to exit.");
        newValue = scanner.nextFloat();
        if(newValue>0){
            currentScore = (currentScore*currentCount+newValue)/(++currentCount);
            System.out.println("Your new score is "+currentScore);
        }else{
            System.out.println("Program is exiting");
        }

    }while(newValue >0);
   }
  }