问答详情
源自:8-9 Java 中的 static 使用之静态方法

return的问题

public static int sum() { return score1+score2; }

为什么在这个位置,填写{ sum=score1+score2;   return sum; }不能得到正确结果??

提问者:苍白的尾巴 2015-01-27 11:35

个回答

  • yymooc
    2015-01-27 11:48:52
    已采纳

    在你的代码里返回的sum是sum()方法里的变量,所有的变量都要先定义在使用,你这里还没有定义sum变量呢

    如果这样写就对了

    int sum=score1+score2;

          return sum;


  • _Exception
    2015-01-27 14:26:04

    int sum = score1 + score2;

            return sum;

    可以啊 

  • 有一种爱叫KNA
    2015-01-27 12:57:36

    static怎么样,知道吗?成员变量还声明为static?

    static用来声明常量的。。