简体   繁体   中英

java returning and calling variables

I am new to java and I am trying to make this bmi calculator but I am having trouble returning and calling variables. I am sure that I am doing something very wrong but have been unable to figure out how to properly do this after searching the internet my guess is I do not know what I should be searching. I will post the code, I am getting 4 errors in my main that are as follows:

required: double,double,double,double
found: no arguments
reason: actual and formal argument lists differ in length

I am assuming that I have improperly set up my variables but could really use a bit of guidance. Thank you in advance.

import java.util.Scanner;

public class cs210 {
    public double weight;
    public double height;
    public double bmi;
    public double wcal;
    public double mcal;
    public double age;   
    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        // TODO code application logic here
        method1 ();
        method2 ();
        method3 ();
        method4 ();
        method5 ();
    }

    public static void method1 () {
        System.out.println ("This program implements a Health Assistance Calculator ");
        System.out.println ("Given a weight, height, and age, it will compute:\n");
        System.out.println ("BMI - Body Mass Index");
        System.out.println ("Calories needed per day to maintain weight");
    }

    public double method2 (double weight, double height, double wcal, double bmi) {

        Scanner keyboard = new Scanner (System.in);

        System.out.println ("Please enter your weight:");
        weight = keyboard.nextDouble ();
        System.out.println ("Press 1 if weight was entered in Kg \n Press 2 if weight was entered in Lbs");
        double wunits = keyboard.nextDouble();
        if (wunits == 1) {
            System.out.println("Thank you");
        } else if (wunits == 2){
            weight = weight / 2.2;  
            System.out.println("Thank you");
                }  
        else {
            System.out.println ("Please try again");
            return 0;
        } 
        System.out.println("Please enter your height:"); 
        height = keyboard.nextDouble ();
        System.out.println ("Press 1 if height was entered in meters \n Press 2 if height was entered in inches");
        int hunits = keyboard.nextInt();
        if(hunits ==1) {
            System.out.println("Thank you");
        } else if (hunits == 2){
            height = height / 0.0254;
        }else {
            System.out.println("Please try again");
            return 0;
        }
        System.out.println("Please enter your age in years:");
        age = keyboard.nextDouble ();
        bmi = weight / Math.pow(height, height); 

        return ( bmi +  age +  height +  weight);
    }

    public static double method3(double weight, double age, double height) {
        double paf = 1.375;
        double mcal;
        mcal = (13.397 * weight + 4.799 * height + 5.677 * age + 88.362) * paf;
        return mcal;
    }

    public static double method4(double weight, double age, double height, double paf){
       double wcal;
       wcal = (93247 * weight + 3.098 * height - 4.330 * age + 447.593) * paf;
       return wcal;
    }

    public double method5(double bmi, double mcal, double wcal){
        System.out.println("Your BMI is:" + bmi);
        System.out.println("A BMI in the range of 18.5 to 24.9 is considered normal\n");
        System.out.println("To maintain your current weight:");
        System.out.println("Men need" + mcal + "per day");
        System.out.println("Women need" + wcal + "per day");
     return 0;

}



    }

You need to pass parameters when you call to methods. If you call to method

public double method2 (double weight, double height, double wcal, double bmi)

You need to call it to like this method2 (50, 2, 200, 25.5);

When you call in to your other methods such as method3, method4, method5 ; you have to give appropriate parameters to those. But when it comes to your method1 . It will not expecting any parameters so you don't want to pass any parameter to that method.

I think this small document will help you to understand method and arguments. https://docs.oracle.com/javase/tutorial/java/javaOO/arguments.html

Its better to add your BMI logic and method to separate class then within the main method create and object and to the rest of manipulation. Otherwise it will hard to maintain and update properties and when you do it in that way remove your static methods. and use proper names for each and every method.

This code will give compilation errors because you have called methods in wrong way and you have call method2 and method5 within static method.

You define method2 like this:

public double method2 (double weight, double height, double wcal, double bmi) {
    // ...

It has four parameters, all double, just like your error message said. Then you call it like this:

method2 ();

Without any parameters at all, again just like the error message said. Since you defined it with four parameters, every time you call it you need to do it with four parameters. The values you use as parameters will be the values that the variables weight , height , wcal and bmi gets inside the function, and if you don't have any parameters the computer will not know what values to use for those variables and therefore throw an error to complain. So you could, as an example, do it like this:

method2(34.9, 23.4, 23.5, 34.1); // Just picked four random numbers here.

But looking at the structure of your program, it looks like you don t want to pass any values to the function at all (since you let the user enter the values inside the function). Then you could just get rid of the parameters, and declare the variables inside the function:

public double method2 () {
    double weight, height, wcal, bmi;
    // ...

Now the variables will be available inside method2 , but not anywhere else. If you want to use the same values later in the other functions, you could instead of declaring them inside your function declare them in your class, and they will become available anywhere in your class, but not anywhere else.

You will have to fix the same issue with the parameters for method3 , method4 and method5 as well.

in method2() you have given 4 parameter but you are not using even a single parameter because you are getting from user. like your modified code is

import java.util.Scanner;

public class cs21`enter code here`0 {
    public static double weight;
    public static double height;
    public static double bmi;
    public double wcal;
    public double mcal;
    public static double age;   
    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        // TODO code application logic here
        cs210 cs=new cs210();
        method1 ();
        double val=cs.method2 ();
        double value1=cs.method3 (weight,age,height);
        double value2=cs.method4 (weight,age,height);
        cs.method5 (bmi,value1,value2);
    }

    public static void method1 () {
        System.out.println ("This program implements a Health Assistance Calculator ");
        System.out.println ("Given a weight, height, and age, it will compute:\n");
        System.out.println ("BMI - Body Mass Index");
        System.out.println ("Calories needed per day to maintain weight");
    }

    public double method2 () {

        Scanner keyboard = new Scanner (System.in);

        System.out.println ("Please enter your weight:");
        weight = keyboard.nextDouble ();
        System.out.println ("Press 1 if weight was entered in Kg \n Press 2 if weight was entered in Lbs");
        double wunits = keyboard.nextDouble();
        if (wunits == 1) {
            System.out.println("Thank you");
        } else if (wunits == 2){
            weight = weight / 2.2;  
            System.out.println("Thank you");
                }  
        else {
            System.out.println ("Please try again");
            return 0;
        } 
        System.out.println("Please enter your height:"); 
        height = keyboard.nextDouble ();

        System.out.println ("Press 1 if height was entered in meters \n Press 2 if height was entered in inches");
        int hunits = keyboard.nextInt();
        if(hunits ==1) {
            System.out.println("Thank you");
        } else if (hunits == 2){
            height = height / 0.0254;
        }else {
            System.out.println("Please try again");
            return 0;
        }
        System.out.println("Please enter your age in years:");
        age = keyboard.nextDouble ();
        bmi = weight / Math.pow(height, height); 

        return ( bmi +  age +  height +  weight);
    }

    public static double method3(double weight, double age, double height) {
        double paf = 1.375;
        double mcal;
        mcal = (13.397 * weight + 4.799 * height + 5.677 * age + 88.362) * paf;
        return mcal;
    }

    public static double method4(double weight, double age, double height){
       double wcal;
       double paf=1.375;
       wcal = (93247 * weight + 3.098 * height - 4.330 * age + 447.593) * paf;
       return wcal;
    }

    public void method5(double bmi, double mcal, double wcal){
        System.out.println("Your BMI is:" + bmi);
        System.out.println("A BMI in the range of 18.5 to 24.9 is considered normal\n");
        System.out.println("To maintain your current weight:");
        System.out.println("Men need" + mcal + "per day");
        System.out.println("Women need" + wcal + "per day");


    }
}

Actually to make your code work the way it is written you should:

  • make all the fields and methods static
  • remove parameters from all methods declarations
  • declare local variable paf in method4 .

That being said the code in that form is quite ugly. You should think about the following improvements:

  • class name should start from capital letter
  • class name should be something meaningful (eg BcmCalculator )
  • fields should be private
  • method should have meaningful names ( printGreetings , readUsersAttributes , etc)
  • in main method you should create instance of the class and call its methods
  • paf should be a constant (ie field private static final double PAF = 1.375; ).

There are further possible improvements, but this should be enough for the beginning.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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