测试分数和 Java 等级

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18756514/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 10:47:28  来源:igfitidea点击:

Test Scores and Grade Java

javacompiler-errors

提问by user2771077

I keep getting these 6 errors:

我不断收到这 6 个错误:

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

TestScoresAndGrade.java: 41: incompatible types

found: char

required: java.lang.String

grade = '?'; TestScoresAndGrade.java: 43: incompatible types

found : char

required: java.lang.String

grade = 'A'; TestScoresAndGrade.java: 45: incompatible types

found : char

required: java.lang.String

grade = 'B';

TestScoresAndGrade.java: 47: incompatible types

found : char

required: java.lang.String

grade = 'C'; TestScoresAndGrade.java: 49: incompatible types

found : char

required: java.lang.String

grade = 'D'; TestScoresAndGrade.java: 51: incompatible types

found : char

required: java.lang.String

grade = 'F'; 6 errors

----jGRASP wedge: exit code for process is 1. ----jGRASP: operation complete.

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

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

发现:字符

需要:java.lang.String

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

发现:字符

需要:java.lang.String

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

发现:字符

需要:java.lang.String

等级 = 'B';

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

发现:字符

需要:java.lang.String

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

发现:字符

需要:java.lang.String

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

发现:字符

需要:java.lang.String

等级 = 'F'; 6 错误

----jGRASP 楔子:进程退出代码为 1。----jGRASP:操作完成。

from

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

回答by Kon

You are assigning character literals to your strings when you are using single quotes.'

当您使用单引号时,您将字符文字分配给您的字符串。

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

It's just one of those things you have to memorize about Java. Characters are single quotes, Strings are double.

这只是您必须记住的有关 Java 的事情之一。字符是单引号,字符串是双引号。

But keep in mind, Strings are objects and must be compared properly. You cannot just do s == rfor strings. To compare strings, write

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

grade.equals("A");

回答by Klas Lindb?ck

String literals use double quotes, so:

字符串文字使用双引号,因此:

grade = "A";

回答by Mureinik

grade is defined as a String, not a char - you should use String literals, denoted by double quotes.

等级被定义为字符串,而不是字符 - 您应该使用字符串文字,用双引号表示。

E.g., instead of grade = 'A', you should use grade = "A".

例如,grade = 'A'您应该使用grade = "A".

回答by d'alar'cop

Either change

要么改变

String grade; to char grade;

OR

或者

Change

改变

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

回答by Juned Ahsan

incompatible types

不兼容的类型

error is happening because you are trying to assign a char value to a String:

发生错误是因为您试图将字符值分配给字符串:

     grade = '?';

You need to use double quotes("") with Stringinstead of single quotes('). Single quotes are used for chartypes.

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

Change this and other strings accordingly form :

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

     grade = '?';

to

     grade = "?";

or you can change the gradetype to charinstead of String.

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

回答by gjman2

use double quote instead of single quote for your grade values. Try

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

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

This is because grade is Stringnot char. " " for Stringand ' ' is for char

这是因为等级String不是char。" " forString和 ' ' 是为了char

回答by Krsna Chaitanya

Please change the grade data type to char or put your literals in double quotes instead of single quotes as shown below

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

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