获得平均数字

时间:2016-04-24 07:28:22

标签: java

我应该计算三个双数的总和并得到它的平均值。 我需要使用以下两种方法,不要更改它们: getNumbers()只获取用户输入,无参数,无返回。 getAverage()计算三个双数的平均值,返回平均值,没有参数。

我的问题是,每次我在CMD上运行它时,都表明该方法不起作用,不会打印输出。让我输入三个数字,并在最后得到它的平均值。

如果有人可以就我的错误给出建议,我们将不胜感激。

import java.util.Scanner;
public class ComputeAverage{
    double firstNum;
    double secondNum;
    double thirdNum; 
    double sum;
    double average;

    public void getNumbers(){
        Scanner keyboard = new Scanner(System.in);
        System.out.println("Enter your first number: ");
        double firstNum = keyboard.nextDouble();
        System.out.println("Enter your second number: ");
        double secondNum = keyboard.nextDouble();
        System.out.println("Enter your third number: ");
        double thirdNum = keyboard.nextDouble();
        Test.println("The average is: " + average);
    }
    public double getAverage(double firstNum, double secondNum, double thirdNum){
        double average = firstNum + secondNum + thirdNum / 3; 
        return average;  

    }
}

4 个答案:

答案 0 :(得分:4)

虽然您应该了解operator precedence for Java,但这来自BOMDAS,适用于maths

/运算符的优先级高于+,这意味着您拥有的是

double average = firstNum + secondNum + (thirdNum / 3); 

很可能是你的意图

 double average = (firstNum + secondNum + thirdNum) / 3; 

我也怀疑你也想调用这个方法。

 Test.println("The average is: " + getAverage(firstNum, secondNum, thirdNum);

我还会将所有字段转换为局部变量以避免混淆。注意:您的IDE应该提示您执行此操作并为您提供自动修复功能。

答案 1 :(得分:1)

您的代码中存在多个问题。首先,它不会编译,因为Test是未知的。您还将firstNumsecondNumthirdNum声明为成员变量,但它们也在getNumbers()中声明为局部变量。您必须决定使用成员变量,或将值作为参数传递给getAverage()

接下来的事情是/优先于+,因此将首先计算thirdNum / 3

没有必要,但我认为最好输入/ 3.0,因为3无论如何都要转换为双倍,只是为了尽可能清楚。

例如,它可能是这样的:

import java.util.Scanner;

public class ComputeAverage{
    double firstNum;
    double secondNum;
    double thirdNum;

    public void getNumbers(){
        Scanner keyboard = new Scanner(System.in);
        System.out.println("Enter your first number: ");
        firstNum = keyboard.nextDouble();
        System.out.println("Enter your second number: ");
        secondNum = keyboard.nextDouble();
        System.out.println("Enter your third number: ");
        thirdNum = keyboard.nextDouble();
    }
    public double getAverage(){
        return (firstNum + secondNum + thirdNum) / 3.0;

    }

    public static void main(String[] args) {
        ComputeAverage ca = new ComputeAverage();
        ca.getNumbers();
        System.out.println("The average is: " + ca.getAverage());
    }
}

答案 2 :(得分:0)

在这里,你没有主要的方法,这是程序的切入点。并且你已经在getNumber()中初始化了3个局部变量,这对getAverage()

是不可见的
import java.util.Scanner;
    public class ComputeAverage{
        double firstNum;
        double secondNum;
        double thirdNum; 
        double sum;
        double average;

        public void getNumbers(){
            Scanner keyboard = new Scanner(System.in);
            System.out.println("Enter your first number: ");
            this.firstNum = keyboard.nextDouble();
            System.out.println("Enter your second number: ");
           this.secondNum = keyboard.nextDouble();
            System.out.println("Enter your third number: ");
            this.thirdNum = keyboard.nextDouble();

        }
        public double getAverage(double firstNum, double secondNum, double thirdNum){
            double average = (firstNum + secondNum + thirdNum )/ 3; 
            return average;  

        }

    public static void main (String [] arg){

    getNumbers();

    getAverage( firstNum, secondNum, thirdNum);

      Test.println("The average is: " + average);
    }
    }

答案 3 :(得分:0)

要使此代码正常工作,您需要考虑3件事

  1. 需要调用System.out.println,或者在类Test中定义静态方法println(你没有发布它,所以我猜你没有它)
  2. 平均值不会自动更新为值,您需要调用您定义的方法... getAverage
  3. 不要忘记getAverage
  4. 中的运算符优先级

    总结,你需要:

    替换这个:

    Test.println("The average is: " + average);
    

    System.out.println("The average is: " + getAverage(firstNum, econdNum,  thirdNum));
    

    并且由于运算符优先级,请执行以下操作:

    public double getAverage(double firstNum, double secondNum, double thirdNum){
            double average = (firstNum + secondNum + thirdNum) / 3; 
            return average;  
    
        }