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()里了吗?请大家解释一下可以吗?小弟感激不尽!谢谢!

解决方案 »

  1.   

    main也只是一个特殊的方法。
    如果要在方法中写方法,最好用个内部类吧。
      

  2.   

    最好是在main方法中调用其他方法,不要嵌套方法了
      

  3.   

    你是从Pascal转过Java来的吧?!
    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));
    }
    }
      

  4.   

    重新写个静态方法然后再Main中调用它
      

  5.   

    倒,那不是说要运行时创建方法么?没有见过呢。
    如果你非要问为什么,那只能说,java不支持这样。你可以用其他的方法实现这个要求。例如嵌套类。