Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/rust/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 运行此代码时出现问题,我';我得到一个.class错误_Java_For Loop_Return Value - Fatal编程技术网

Java 运行此代码时出现问题,我';我得到一个.class错误

Java 运行此代码时出现问题,我';我得到一个.class错误,java,for-loop,return-value,Java,For Loop,Return Value,我在运行时遇到问题 我无法使返回值正常工作。这门课我已经上了几个星期了,我学到了很多东西,但是这个项目对我来说有点困难 我正在收集数据,创建一个平均值,然后使用该平均值输出相应的字母等级 任何帮助都将不胜感激 import java.util.*; public class LetterGrade { public static void main(String args[]) { calculateAvg();

我在运行时遇到问题

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

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

任何帮助都将不胜感激

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

       }
    }

签出您的
main
功能。必须将变量正确分配给
calculateAvg
方法的返回值。试试这个:

public static void main(String args[]) {

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

}
另请注意:double是变量的类型,而不是名称。您必须在
printLetter()
方法中输入名称

我刚刚发现的另一个问题是
avgScore
变量的双重赋值,您在
calculateAvg()
方法中使用了该变量。删除此行:

avgScore = console.nextInt();

这将迫使用户再次键入一个值,然后将该值分配给变量avgScore。这是没有必要的

我希望有一些评论对您有用: 1-将main方法中的代码更改为
double avgScore=calculateAvg();
打印信函(avgScore)
2-检查for循环的使用情况(可能需要do-while循环)
3-以“printLetter”方法卸下扫描仪

4-也如@NiklasLehnfeld建议删除
avgscore=console.nextInt()

你犯了很多错误

在main方法中,您应该将计算出的平均值保存到变量中,并将其传递给
printLetter
方法,如下所示

double avgScore = calculateAvg();
printLetter(avgScore);
public static double calculateAvg()
calculateAvg
方法返回类型应该是
double
,因此您必须这样声明它

double avgScore = calculateAvg();
printLetter(avgScore);
public static double calculateAvg()
另外,
calculateAvg
中的for循环是错误的,不必要的,因此只需删除它即可。将
scanner.nextInt()
值分配给
avgScore
将丢弃正确计算的值。因此,
calculateAvg
方法应该是这样的

double avgScore = calculateAvg();
printLetter(avgScore);
public static double 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);
    }
}

尝试将主方法更改为:

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

您的程序中有几个错误

我已通过评论更正了程序:

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 曼尼莎,你的平均考试成绩是:13.7,平均分是:F