测试分数和 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
Test Scores and Grade Java
提问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 == r
for 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 String
instead of single quotes('
). Single quotes are used for char
types.
您需要使用双引号 ( ""
)String
而不是单引号 ( '
)。单引号用于char
类型。
Change this and other strings accordingly form :
相应地更改此字符串和其他字符串的形式:
grade = '?';
to
到
grade = "?";
or you can change the grade
type to char
instead 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 String
not char
. " " for String
and ' ' 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";