请看一下这个主题: 我可以计算这些数字的平均值吗? 我该如何为此编写程序?在正确的答案中,他只是在没有 java 程序的情况下编写了方程式,例如他使用了 newValue 而没有初始化 newValue。这是等式:
int currentScore = (currentScore * currentCount + newValue) / currentCount;
请看一下这个主题: 我可以计算这些数字的平均值吗? 我该如何为此编写程序?在正确的答案中,他只是在没有 java 程序的情况下编写了方程式,例如他使用了 newValue 而没有初始化 newValue。这是等式:
int currentScore = (currentScore * currentCount + newValue) / currentCount;
请找到以下损坏的步骤:
private float calculateNewScore(){
float currentScore = 6.1123
int currentCount = 12;
float newValue = 4.5;
int newCount = currentCount+1;
float newScore = (currentScore * currentCount + newValue) / newCount ;
}
如果您正在寻找示例程序,它可能是这样的:
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);
}
}