运行此代码时遇到问题,我遇到了.class错误

时间:2020-04-17 22:31:44

标签: java for-loop return-value

我在运行此程序时遇到问题。

我无法设法使返回值正常工作。我已经上了几周课程,并且学到了很多东西,但是这个项目对我来说有点困难。

我正在收集数据,创建平均值,然后使用该平均值输出相应的字母等级。

任何帮助将不胜感激。

   import java.util.*;
    public class LetterGrade
    {
       public static void main(String args[]) 
       {
         calculateAvg();
         double avgScore;
         printLetter(double);


        }
        public static void calculateAvg()
           {      
              Scanner console = new Scanner(System.in);
              double avgScore; 

          for (double i = 0; i >0; i++) 
          {
              System.out.println("Enter your full name.");
              String name = console.nextLine();
              System.out.println("Enter test score 1");
              double score1 = console.nextDouble();
              System.out.println("Enter test score 2");
              double score2 = console.nextDouble();
              System.out.println("Enter test score 3");
              double score3 = console.nextDouble();

              avgScore = (score1 + score2 + score3) / 3;
              avgScore = console.nextInt();

              System.out.printf( "%s, your average test score is: %.1f",name,avgScore);

                return avgScore;
              }
         }

            public static void printLetter(double avgScore)
            {
                    Scanner console = new Scanner(System.in);
                    char grade;

                 if (avgScore >= 90)
                 {
                      grade = 'A';
                  }
                  else if (avgScore >= 80)
                  {
                     grade = 'B';
                  }
                  else if (avgScore >= 70)
                  {
                     grade = 'C';
                  }
                  else if (avgScore >= 60)
                  {
                     grade = 'D';
                  }
                  else
                  {
                     grade = 'F';
                 }

                    System.out.println("With that average, your grade is: " + grade);

       }
    }

5 个答案:

答案 0 :(得分:1)

程序中有几个错误。

我已通过注释更正了该程序:

import java.util.*;
public class LetterGrade
{
    public static void main(String args[]) 
    {

        double avgScore=  calculateAvg();
        printLetter(avgScore); //pass variable name here not datatype


    }
    public static double calculateAvg() //must have return type
    {      
        Scanner console = new Scanner(System.in);
        double avgScore; 

        // for (double i = 0; i 0; i++) 
        //{
        System.out.println("Enter your full name.");
        String name = console.nextLine();
        System.out.println("Enter test score 1");
        double score1 = console.nextDouble();
        System.out.println("Enter test score 2");
        double score2 = console.nextDouble();
        System.out.println("Enter test score 3");
        double score3 = console.nextDouble();

        avgScore = (score1 + score2 + score3) / 3;
        // avgScore = console.nextInt();

        System.out.printf( "%s, your average test score is: %.1f",name,avgScore);

        return avgScore;
        // }
    }

    public static void printLetter(double avgScore)
    {
        //Scanner console = new Scanner(System.in);
        char grade;

        if (avgScore >= 90)
        {
            grade = 'A';
        }
        else if (avgScore >= 80)
        {
            grade = 'B';
        }
        else if (avgScore >= 70)
        {
            grade = 'C';
        }
        else if (avgScore >= 60)
        {
            grade = 'D';
        }
        else
        {
            grade = 'F';
        }

        System.out.println("With that average, your grade is: " + grade);

    }
}

输出:
输入您的全名。 马尼沙 输入考试成绩1 12 输入考试成绩2 14 输入考试成绩3 15 manisha,您的平均考试成绩是:13.7有了这个平均值,您的成绩是:F

答案 1 :(得分:0)

签出您的main函数。您必须将变量正确分配给calculateAvg方法的返回值。试试看:

public static void main(String args[]) {

    double avgScore = calculateAvg();
    printLetter(avgScore);

}

还要注意: double是变量的类型,而不是名称。您必须将名称输入printLetter()方法中。

我刚刚发现的另一个问题是您在avgScore方法中拥有的calculateAvg()变量被加倍分配。删除此行:

avgScore = console.nextInt();

这将迫使用户再次键入一个值,然后将其分配给变量avgScore。这不是必需的。

答案 2 :(得分:0)

我希望对您有所帮助: 1-将main方法中的代码更改为 double avgScore = calculateAvg(); printLetter(avgScore); 2-检查for循环的使用(也许您需要执行一次while循环) 3-以“ printLetter”方法取出扫描仪 4-也正如@NiklasLehnfeld建议删除avgscore = console.nextInt();

答案 3 :(得分:0)

您犯了很多错误。

在main方法中,应将计算出的平均值保存到变量中,然后将其传递给printLetter方法

double avgScore = calculateAvg();
printLetter(avgScore);

calculateAvg方法的返回类型应为double,因此您必须像这样声明它

public static double calculateAvg()

calculateAvg中的for循环也是错误且不必要的,因此只需将其删除即可。将scanner.nextInt()的值分配给avgScore将丢弃正确计算的值。所以calculateAvg方法应该像这样

printLetter方法正确,但包含不必要的行,如

Scanner console = new Scanner(System.in); //Could be removed

结果代码为

import java.util.*;

public class LetterGrade
{
    public static void main(String args[])
    {
        double avgScore = calculateAvg();
        printLetter(avgScore);
    }

    public static double calculateAvg()
    {
        Scanner console = new Scanner(System.in);
        double avgScore;

        System.out.println("Enter your full name.");
        String name = console.nextLine();
        System.out.println("Enter test score 1");
        double score1 = console.nextDouble();
        System.out.println("Enter test score 2");
        double score2 = console.nextDouble();
        System.out.println("Enter test score 3");
        double score3 = console.nextDouble();

        avgScore = (score1 + score2 + score3) / 3;

        System.out.printf( "%s, your average test score is: %.1f\n", name, avgScore);

        return avgScore;
    }

    public static void printLetter(double avgScore)
    {
        char grade;

        if (avgScore >= 90)
        {
            grade = 'A';
        }
        else if (avgScore >= 80)
        {
            grade = 'B';
        }
        else if (avgScore >= 70)
        {
            grade = 'C';
        }
        else if (avgScore >= 60)
        {
            grade = 'D';
        }
        else
        {
            grade = 'F';
        }

        System.out.println("With that average, your grade is: " + grade);
    }
}

答案 4 :(得分:0)

尝试将主要方法更改为此:

public static void main(String args[]) {
    double avgScore = calculateAvg();
    printLetter(avgScore);
}