user1730056
user1730056

Reputation: 623

Problems with my first Java program

I'm trying to make a simple pounds to kilogram converter. Not sure what I'm doing wrong because it won't print out the answer. Could someone help me out.

public class ass10 {

    public static void main(String[] args) {
    double lbs2kg(3);
    }
    public double lbs2kg(double w){
        System.out.println(w/2.2);
    }
}

Upvotes: 3

Views: 115

Answers (5)

Rahul Tripathi
Rahul Tripathi

Reputation: 172618

use something like this:-

  double x = lbs2kg(3);

You are also missing the return in your function.

Also,

public static void lbs2kg(double w){
System.out.println(w/2.2);
}

Upvotes: 1

user1730056
user1730056

Reputation: 623

turns out I'm just missing static for my method. Thanks for the help!

Upvotes: 0

beatgammit
beatgammit

Reputation: 20225

For cleaner code, I'd do:

public class ass10 {
    public static void main(String[] args) {
        System.out.println(lbs2kg(3));
    }

    public static double lbs2kg(double w){
        return w/2.2;
    }
}

Upvotes: 0

alex
alex

Reputation: 490597

You probably want...

public class ass10 {

    public static void main(String[] args) {
        lbs2kg(3);
    }

    public static void lbs2kg(double w){
        System.out.println(w/2.2);
    }
}

Upvotes: 0

Juvanis
Juvanis

Reputation: 25950

Delete double or put a variable and also method lbs2kg() must be static (Make it return double or a compatible type, too).

public static void main(String[] args) {
    double x = lbs2kg(3);
}

Upvotes: 3

Related Questions