Error in java method?
I have some simp开发者_运维知识库le java code to call a method with a value to calculate the volume. I just get error like missing ; in JCreator? What is wrong? This is new beginners non object programming level course. Therefore i guess there should be no public static in the method?
public class Matematik {
public static void main(String[] args) {
System.out.println(volym(10));
double volym(int tal){
return round((4 * math.pi * math.pow(tal,3) / 3),2);
}
}
}
The declaration for volym should not be in main:
public class Matematik {
public static double volym(int tal){
return round((4 * math.pi * math.pow(tal,3) / 3),2);
}
public static void main(String[] args) {
System.out.println(volym(10));
}
}
Edit: it's worth noting that you have other issues too. Namely, java.lang.Math.PI (note the casing) and Math.pow. And Math.round....
You can't define a method inside a method. This would be more obvious if you use the IDEs code formatting. Move one of the }
up to before the second method (BTW it must be static as well)
Also its Math
not math
and Math.round
on take one value which it rounds to an integer.
If you want to round to two decimal places you can do
Math.round(x * 100) / 100.0;
精彩评论