测试分数和成绩Java

时间:2013-09-12 06:05:24

标签: java compiler-errors

我不断收到这6个错误:

  

---- jGRASP exec:javac -g TestScoresAndGrade.java

     

TestScoresAndGrade.java:41:不兼容的类型

     

发现:char

     

必需:java.lang.String

     

grade ='?';                     TestScoresAndGrade.java:43:不兼容的类型

     

发现:char

     

必需:java.lang.String

     

grade ='A';                    TestScoresAndGrade.java:45:不兼容的类型

     

发现:char

     

必需:java.lang.String

     

grade ='B';

     

TestScoresAndGrade.java:47:不兼容的类型

     

发现:char

     

必需:java.lang.String

     

grade ='C';                     TestScoresAndGrade.java:49:不兼容的类型

     

发现:char

     

必需:java.lang.String

     

grade ='D';                     TestScoresAndGrade.java:51:不兼容的类型

     

发现:char

     

必需:java.lang.String

     

grade ='F';                    6个错误

     

---- jGRASP wedge:进程的退出代码是1. ---- jGRASP:operation   完整。

import java.util.Scanner;

public class TestScoresAndGrade
{
   public static void main(String[] args)
   {

    Scanner keyboard = new Scanner(System.in); 

      int score1;
      int score2;
      int score3;
      double ScoreAvg;
      String grade;


      System.out.print("Enter the first score: ");
                score1 = keyboard.nextInt();

        System.out.print("Enter the second score: ");
                score2 = keyboard.nextInt();

       System.out.print("Enter the third score: ");
                score3 = keyboard.nextInt();

        ScoreAvg = (score1 + score2 + score3)/ 3.0;
      ScoreAvg = Math.round(ScoreAvg);

        if(ScoreAvg > 100)
             grade = '?';
        else if(ScoreAvg >= 90)
         grade = 'A';
      else if(ScoreAvg >= 80)
         grade = 'B';
        else if(ScoreAvg >= 70)
         grade = 'C';
      else if(ScoreAvg >= 60)
         grade = 'D';
      else
         grade = 'F';

      System.out.println("Average score: " + ScoreAvg + " " + grade + "\n");
   }
}

7 个答案:

答案 0 :(得分:3)

为您的成绩值使用双引号而不是单引号。尝试

import java.util.Scanner;

public class TestScoresAndGrade
{
   public static void main(String[] args)
   {

    Scanner keyboard = new Scanner(System.in); 

      int score1;
      int score2;
      int score3;
      double ScoreAvg;
      String grade;


      System.out.print("Enter the first score: ");
                score1 = keyboard.nextInt();

        System.out.print("Enter the second score: ");
                score2 = keyboard.nextInt();

       System.out.print("Enter the third score: ");
                score3 = keyboard.nextInt();

        ScoreAvg = (score1 + score2 + score3)/ 3.0;
      ScoreAvg = Math.round(ScoreAvg);

        if(ScoreAvg > 100)
             grade = "?";
        else if(ScoreAvg >= 90)
         grade = "A";
      else if(ScoreAvg >= 80)
         grade = "B";
        else if(ScoreAvg >= 70)
         grade = "C";
      else if(ScoreAvg >= 60)
         grade = "D";
      else
         grade = "F";

      System.out.println("Average score: " + ScoreAvg + " " + grade + "\n");
   }
}

这是因为成绩为String而不是char。 “String”和“char

答案 1 :(得分:1)

  

不兼容的类型

错误正在发生,因为您正在尝试将char值分配给String:

     grade = '?';

您需要对""使用双引号(String)而不是单引号(')。单引号用于char类型。

相应地更改此字符串和其他字符串:

     grade = '?';

     grade = "?";

或者您可以将grade类型更改为char而不是String

答案 2 :(得分:0)

当您使用单引号时,您正在为字符串分配字符文字。'

grade = 'A'; //not legal
grade = "A"; //legal

这只是你必须记住的关于Java的事情之一。字符是单引号,字符串是双精度。

但请记住,字符串是对象,必须正确比较。你不能只为字符串做s == r。要比较字符串,请写

grade.equals("A");

答案 3 :(得分:0)

字符串文字使用双引号,所以:

grade = "A";

答案 4 :(得分:0)

grade被定义为String,而不是char - 你应该使用String literals,用双引号表示。

例如,您应该使用grade = 'A'而不是grade = "A"

答案 5 :(得分:0)

要么改变

String grade; to char grade;

OR

更改

grade = 'F'; to grade = "F";

答案 6 :(得分:0)

请将成绩数据类型更改为字符或将您的文字换成双引号而不是单引号,如下所示

if(ScoreAvg > 100)
             grade = "?";
        else if(ScoreAvg >= 90)
         grade = "A";