首页 文章

错误消息:“错误:在类Conditionals中找不到主要方法,请将main方法定义为:public static void main(String [] args)”[关闭]

提问于
浏览
-4

我已经拥有的代码如下 . 我试图输入一个double值并返回一个char值用于评分目的 . 在jedit中使用java代码 . 继续收到错误消息“错误:在类Conditionals中找不到主要方法,请将main方法定义为:public static void main(String [] args)”

public class Conditionals
{
public static double letterGrade(double score){
char result;
if (score >= 90.0)
{ result = 'A'; }
else if (score >= 80.0) 
{ result = 'B'; }
else if (score >= 70.0) 
{ result = 'C'; }
else if (score >= 60.0)
{ result = 'D'; }
else 
{ result = 'F'; }
return result;
}
}

这是新代码:

public class Conditionals
{public static void main(String []args){
System.out.println(letterGrade(60.0));
}
public static double letterGrade(double score){

char result;
if (score >= 90.0)
{ result = 'A'; }
else if (score >= 80.0) 
{ result = 'B'; }
else if (score >= 70.0) 
{ result = 'C'; }
else if (score >= 60.0)
{ result = 'D'; }
else 
{ result = 'F'; }
return result;


}
}

但是当我输入60.0时它返回68.0

司机班:

public class ConditionalTest
public static main(String[] args)

据我所知

2 回答

  • 8

    您需要在程序中使用Main方法,以便 JVM 可以开始执行或执行您的代码

    主方法的签名是

    public static void main(String[] args)
    

    public 是访问修饰符,因此它在类和其他类中也是可见的 .

    static 表示不需要创建任何对象

    void 表示此函数不返回任何内容

    String[] args 表示命令行参数可以传递给Java程序

    所以这就是代码中缺少的东西 .

    希望能帮助到你

    你遇到的另一个问题是

    public static **double** letterGrade(double score){
    

    the return type has to be char not double

    public static **char** letterGrade(double score){
    
  • 0

    更改

    System.out.println(letterGrade(60.0));
    

    System.out.println(Conditionals.letterGrade(60.0));
    

相关问题