public class a123{
public static void main(String[] args){
double f(double x){
return Math.cos(x)*Math.sin(x)*Math.pow(x,2);
}
double g(double x){
return Math.log(Math.pow(x,10))*Math.cos(Math.sin(x));
}
double h(double x){
return Math.pow(x,10)+Math.pow(x,6)+3*Math.pow(x,3)+Math.pow(Math.E,5);
}
System.out.println("f(1.2)="+f(1.2));
System.out.println("g(1.2)="+g(1.2));
System.out.println("h(1.2)="+h(1.2));
}
}这个程序不能通过,难道方法不能写在public static main()里了吗?请大家解释一下可以吗?小弟感激不尽!谢谢!
public static void main(String[] args){
double f(double x){
return Math.cos(x)*Math.sin(x)*Math.pow(x,2);
}
double g(double x){
return Math.log(Math.pow(x,10))*Math.cos(Math.sin(x));
}
double h(double x){
return Math.pow(x,10)+Math.pow(x,6)+3*Math.pow(x,3)+Math.pow(Math.E,5);
}
System.out.println("f(1.2)="+f(1.2));
System.out.println("g(1.2)="+g(1.2));
System.out.println("h(1.2)="+h(1.2));
}
}这个程序不能通过,难道方法不能写在public static main()里了吗?请大家解释一下可以吗?小弟感激不尽!谢谢!
如果要在方法中写方法,最好用个内部类吧。
Java是不支持方法嵌套的。如果你硬是喜欢在main方法里整点方法出来,可以使用内部类。改了一下你的程序:class a123 {
public static void main(String[] args) {
class calculator {
double f(double x) {
return Math.cos(x) * Math.sin(x) * Math.pow(x, 2);
} double g(double x) {
return Math.log(Math.pow(x, 10)) * Math.cos(Math.sin(x));
} double h(double x) {
return Math.pow(x, 10) + Math.pow(x, 6) + 3 * Math.pow(x, 3)
+ Math.pow(Math.E, 5);
}
}
calculator cclt = new calculator();
System.out.println("f(1.2)=" + cclt.f(1.2));
System.out.println("g(1.2)=" + cclt.g(1.2));
System.out.println("h(1.2)=" + cclt.h(1.2));
}
}
如果你非要问为什么,那只能说,java不支持这样。你可以用其他的方法实现这个要求。例如嵌套类。