本人刚学java两天 编了个程序不知错在哪  帮忙解决下谢谢
public class  Xiti5
{
public static void main (String args[]){
int sum=0,i=0;
System.out.println("计算1!+2!+3!+4!+5!+..的前10和前5项项之差:"+sum);
sum=(int)q(i)+(int)r(i);
}
}class  Xiti5_b
{
public static void main (String args[]){
System.out.println("计算1!+2!+3!+4!+5!+..的前5项之和:"+q(5));
}

public static int q(int n)
{
int m=0;
for(int i =1;i<=n;i++){
m=(int)method(i)+m;
}
return m;
}
public static int method(int n){
if (n!=1){
int method=0;
method=(int)n*method(n-1);
return method;
}
else
{return 1;}
}}
class  Xiti5_a
{
public static void main (String args[]){
System.out.println("计算1!+2!+3!+4!+5!+..的前10项之和:"+r(10));
}

public static int r(int n)
{
int m=0;
for(int i =1;i<=n;i++){
m=(int)method(i)+m;
}
return m;
}
public static int method(int n){
if (n!=1){
int method=0;
method=(int)n*method(n-1);
return method;
}
else
{return 1;}
}}
编译时出现的错误是:Xiti5.java:6: cannot find symbol
                sum=(int)q(i)+(int)r(i);
                         ^
  symbol:   方法 q(int)
  location: 类 Xiti5
Xiti5.java:6: cannot find symbol
                sum=(int)q(i)+(int)r(i);
                                   ^
  symbol:   方法 r(int)
  location: 类 Xiti5
2 错误

解决方案 »

  1.   


    public class Xiti5 {
    public static void main(String args[]) {
    int sum = 0, i = 0;
    sum = (int) q(i) + (int) r(i);
    System.out.println("计算1!+2!+3!+4!+5!+..的前10和前5项项之差:" + sum);
    System.out.println("计算1!+2!+3!+4!+5!+..的前5项之和:" + q(5));
    System.out.println("计算1!+2!+3!+4!+5!+..的前10项之和:" + r(10));

    }
    public static int q(int n) {
    int m = 0;
    for (int i = 1; i <= n; i++) {
    m = (int) method(i) + m;
    }
    return m;
    }

    public static int method(int n) {
    if (n != 1) {
    int method = 0;
    method = (int) n * method(n - 1);
    return method;
    } else {
    return 1;
    }
    }
    public static int r(int n) {
    int m = 0;
    for (int i = 1; i <= n; i++) {
    m = (int) method(i) + m;
    }
    return m;
    }
    }
      

  2.   

    把你的Xiti5_b这样的类修改下,在Xiti5中以这样的形式调用:Xiti5_b.q(i)
    因为q(i)方法是其他类的,Xiti5没有那个方法。
    r(i)一样的。
      

  3.   

    你调用Xiti5_a和Xiti5_b中的静态方法总要有个类名吧,以下是我的修改
    public class Xiti5 {
    public static void main(String args[]) {
    int sum = 0, i = 0;
    sum = (int)Xiti5_b. q(i) + (int)Xiti5_a. r(i);
    System.out.println("计算1!+2!+3!+4!+5!+..的前10和前5项项之差:" + sum);

    }
    }class Xiti5_b {
    public static void main(String args[]) {
    System.out.println("计算1!+2!+3!+4!+5!+..的前5项之和:" + q(5));
    } public static int q(int n) {
    int m = 0;
    for (int i = 1; i <= n; i++) {
    m = (int) method(i) + m;
    }
    return m;
    } public static int method(int n) {
    if (n != 1) {
    int method = 0;
    method = (int) n * method(n - 1);
    return method;
    } else {
    return 1;
    }
    }}class Xiti5_a {
    public static void main(String args[]) {
    System.out.println("计算1!+2!+3!+4!+5!+..的前10项之和:" + r(10));
    } public static int r(int n) {
    int m = 0;
    for (int i = 1; i <= n; i++) {
    m = (int) method(i) + m;
    }
    return m;
    } public static int method(int n) {
    if (n != 1) {
    int method = 0;
    method = (int) n * method(n - 1);
    return method;
    } else {
    return 1;
    }
    }}