简体   繁体   English

运行此代码时出现问题,我收到了 a.class 错误

[英]Having issues running this code, I'm getting a .class error

I'm having issues getting this to run.我在运行它时遇到问题。

I can't manage to get the return value to work properly.我无法让返回值正常工作。 I'm a few weeks into this course and I'm learning a lot but this project has been a little bit of a struggle for me.我已经进入这门课程几周了,我学到了很多东西,但是这个项目对我来说有点困难。

I'm collecting data, creating an average and then using that average to output the corresponding letter grade.我正在收集数据,创建一个平均值,然后将该平均值用于 output 相应的字母等级。

Any help would be appreciated.任何帮助,将不胜感激。

   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);

       }
    }

There are several errors in your program.您的程序中有几个错误。

I have corrected the program with comments:我已经用评论更正了程序:

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);

    }
}

OUTPUT: OUTPUT:
Enter your full name.输入您的全名。 manisha Enter test score 1 12 Enter test score 2 14 Enter test score 3 15 manisha, your average test score is: 13.7With that average, your grade is: F manisha 输入测试分数 1 12 输入测试分数 2 14 输入测试分数 3 15 manisha,你的平均测试分数是:13.7 有了这个平均值,你的成绩是:F

Checkout your main function.检查您的main function。 You have to assign the variable correctly to the return value of the calculateAvg method.您必须将变量正确分配给calculateAvg方法的返回值。 Try this out:试试这个:

public static void main(String args[]) {

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

}

Also notice: double is the type of the variable, not the name.另请注意: double 是变量的类型,而不是名称。 You have to give the name into the printLetter() method.您必须将名称输入printLetter()方法。

Another issue I just found is the doubled assigning of the avgScore variable which you have in your calculateAvg() method.我刚刚发现的另一个问题是您在calculateAvg()方法中的avgScore变量的双重分配。 Remove this line:删除这一行:

avgScore = console.nextInt();

This would force the user to again type in a value, which gets then assigned to the variable avgScore.这将迫使用户再次输入一个值,然后将其分配给变量 avgScore。 This is not necessary.这不是必需的。

There are some comments that I hope to be useful for you: 1- change the code in the main method to be like that double avgScore = calculateAvg(); printLetter(avgScore);有一些评论希望对您有用: 1- 将 main 方法中的代码更改为double avgScore = calculateAvg(); printLetter(avgScore); double avgScore = calculateAvg(); printLetter(avgScore); 2- check the use of the for loop (maybe you need a do while loop) 3- remove the scanner in "printLetter" method 4- also as @NiklasLehnfeld suggest to remove avgscore = console.nextInt(); 2-检查for循环的使用(也许你需要一个do while循环)3-在“printLetter”方法中删除扫描仪4-同样@NiklasLehnfeld建议删除avgscore = console.nextInt();

You made many mistakes.你犯了很多错误。

In the main method you should save the calculated average into the variable and pass it to the printLetter method like this在 main 方法中,您应该将计算的平均值保存到变量中并将其传递给printLetter方法,如下所示

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

The calculateAvg method return type should be double so you have to declare it like this calculateAvg方法的返回类型应该是double所以你必须像这样声明它

public static double calculateAvg()

Also the for loop inside calculateAvg is wrong and unnecessary so just remove it.此外, calculateAvg中的 for 循环也是错误且不必要的,因此只需将其删除即可。 And assigning the scanner.nextInt() value to the avgScore will discard the properly calculated value.并将scanner.nextInt scanner.nextInt()值分配给avgScore将丢弃正确计算的值。 so the calculateAvg method should be like this所以calculateAvg方法应该是这样的

The printLetter method is correct but contains unnecessary lines like printLetter方法是正确的,但包含不必要的行,例如

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

The resulting code is结果代码是

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);
    }
}

Try to change the main method to this:尝试将主要方法更改为:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM