开发者社区 问答 正文

关于方法调用问题

直接上代码啦:问题也在代码的解释里啦:麻烦各位大神啊

public class Method{
public static void main(String[]args){//main方法可以调用main方法所在的类中的定义的其他方法,也可以调用别的类中的方法。(书上这么写的)
System.out.print("The grade is:");
printGrade(78.5);//调用方法时要注意参数的传递,在 这里78.5就是实参(自己的笔记)
System.out.print("The grade is:");
printGrade(59.5);
}
public static void printGrade(double score){ 
    //返回值类型为void的方法,调用时必须是一条语句。那为什么在这个代码里不是用一个语句调用,而是直接方法名调用?(这个就是我的问题啦)

    if(score>=90){
        System.out.println("A");
    }else if(score>=80){
        System.out.println("B");
    }else if(score>=70){
        System.out.println("C");
    }else{
        System.out.println("F");
    }
}
}

展开
收起
蛮大人123 2016-05-26 15:44:05 2014 分享 版权
1 条回答
写回答
取消 提交回答
  • 我说我不帅他们就打我,还说我虚伪

    如果是

    class A
    {
    public static void printGrade(double score){ 
        if(score>=90){
            System.out.println("A");
        }else if(score>=80){
            System.out.println("B");
        }else if(score>=70){
            System.out.println("C");
        }else{
            System.out.println("F");
        }
    }
    }

    在你的main里面调用就不能是printGrade(78.5);必须是A.printGrade(78.5);
    再修改下,去掉static
    那么就不能A.printGrade(78.5);
    必须是new A().printGrade(78.5);

    2019-07-17 19:16:22
    赞同 展开评论
问答地址: