I am real beginner in Java and I have one simple exercise where I need to convert m/h into km/h using a method and a return from it.
I have to define 2 situations: if km/h < 0 return -1 (error) and if km/h > 0 return km/h * 1.609 (value in m/h).
I tried everything I could think of but I either get a no return statement error or no output when I try to run it.
I can't understand why even if I gave it more than one return option it just doesn't work whatever the value is. I could use System.outprintln or String but the exercise specify I must use a return method.
here is my code, written in IntelliJ:
package EXERCISE;
public class Main {
public static void main(String[] args) {
toMilesPerHour(0);
}
public static double toMilesPerHour(double kilometersPerHour) {
if (kilometersPerHour < 0) {
return -1;
}
else if (kilometersPerHour > 0) {
return kilometersPerHour * 1.609d;
}
else if (kilometersPerHour == 0) {
return 0;
}
return kilometersPerHour * 1.609;
// if I don't write return here it gives me no return statement error,
// if I write it, it gives me no output with value > or < 0 but no error.
}
}
public static double toMilesPerHour(double kilometersPerHour) {
if (kilometersPerHour < 0) {
return -1;
}
else {
return kilometersPerHour * 1.609;
}
}
Try it like this:
public static double toMilesPerHour(double kilometersPerHour) {
return (kilometersPerHour > 0 ? kilometersPerHour*1.609 : -1;
}
You could also throw an exception if speed is negative:
public static double toMilesPerHour(double kilometersPerHour) {
if (kilometersPerHour < 0) throw new IllegalArgumentException("speed cannot be negative");
return kilometersPerHour*1.609;
}
Even if you use a method, you have to print the returned value:
package EXERCISE;
public class Main {
public static void main(String[] args) {
System.out.println(toMilesPerHour(0));
}
public static double toMilesPerHour(double kilometersPerHour) {
if (kilometersPerHour < 0) {
return -1;
}
else if (kilometersPerHour > 0) {
return kilometersPerHour * 1.609d;
}
else if (kilometersPerHour == 0) {
return 0;
}
return kilometersPerHour * 1.609;
//if I don't write return here it gives me no return statement error,
//if I write it, it gives me no output with value > or < 0 but no error.
}
}
Furthermore, you can get rid of the return statement at the end:
public static double toMilesPerHour(double kilometersPerHour) {
if (kilometersPerHour < 0) {
return -1;
}
else {
// you don't need to check if kilometersPerHour is 0, since every number multiplied with 0 is 0
return kilometersPerHour * 1.609;
}
}
If you use if()... else you have exactly two options. Either you go into the if clause and do whats there, or you go into the else clause. If if-clause and else-clause both have a return-statement, everything goes fine.
But you don't have if-else-clauses! You have if-else-if-clauses! If the if-clause doesn't hold, you go into the else-clause, which again has an if-clause and so on. Finally you don't have a default else clause.... if you don't get into the first if-clause, you check in the else-clause, if the condition holds. If not, you check the condition in the next else clause. The last return-statement finally is the default else clause. We all, as humans, see, that every condition (<, == >) is covered, but the compiler doesn't see that!
The reason compiler gives you "no return statement" error is because you didn't cover all possible cases with your ifs
: there is Double.NaN
, which is not equal to 0
(or any other value, for that matter, including itself) and neither greater nor lesser than 0
.
Also, compiler doesn't analyze your code deep enough to check if you covered all possible variants anyway: if you replace double
with long
, the result is the same - the compiler will see the reachable branch that doesn't return anything, and so it produces an error. To fix an error, you need to have all branches return something:
if (kilometersPerHour < 0) {
return -1;
}
else if (kilometersPerHour == 0) {
return 0;
}
// Note: I don't have explicit check for NaN,
// because any arithmetic on NaN produces NaN,
// which would be the correct result for this function.
// So I let the "*" operator to check for NaN instead.
return kilometersPerHour * 1.609;
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.