我已经拥有的代码如下。我试图输入一个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)
据我所知
答案 0 :(得分:8)
您需要在程序中使用Main方法,以便JVM
可以开始执行或执行代码
主要方法的签名是
public static void main(String[] args)
public 是访问修饰符,因此它在类和其他类中也是可见的。
静态表示无需创建任何对象
void 表示此函数不返回任何内容
String [] args 表示命令行参数可以传递给Java程序
所以这就是代码中缺少的内容。
希望有所帮助
你遇到的另一个问题是
public static **double** letterGrade(double score){
返回类型必须为char而不是double
public static **char** letterGrade(double score){
答案 1 :(得分:0)
更改
System.out.println(letterGrade(60.0));
到
System.out.println(Conditionals.letterGrade(60.0));