所以在我的第五行代码[if(length == 2 || 1)]
中,我的错误或我的声明说运算符||未定义参数类型boolean,int。关于我的语法有什么问题以及如何解决它的任何想法?谢谢!
//Write a program that translates a letter grade into a number grade. Letter grades are
//A B C D F, possibly followed by + or -. Their numeric values are 4, 3, 2, 1, and 0.
//There is no F+ or F-. A + increases the numeric value by 0.3, a - decreases it by 0.3.
//However, an A+ has the value 4.0. All other inputs have value –1.
//Enter a letter grade:
//使用方法getNumericGrade的等级。
public class Grade {
private double grade = 0.0;
public double getNumericGrade(String letterGrade){
int length = letterGrade.length();
if(length == 2 || 1){
char startChar = letterGrade.charAt(0);
char endChar = letterGrade.charAt(1);
switch(startChar){
case 'A':
this.grade = 4.0;
break;
case 'B':
this.grade = 3.0;
break;
case 'C':
this.grade = 2.0;
break;
case 'D':
this.grade = 1.0;
break;
case 'F':
this.grade = 0.0;
break;
default:
this.grade = -1;
}
if(length == 2){
switch(endChar){
case '-':
this.grade = this.grade - .3;
break;
case '+':
if(startChar != 'A'){
this.grade = this.grade + .3;
}
break;
default:
this.grade = -1;
}
}
if(startChar == 'F' && length != 1){
this.grade = -1;
}
}else{
this.grade = -1;
}
return this.grade;
}
}
答案 0 :(得分:3)
这意味着||
运算符不带布尔值和int,你必须给它两个布尔表达式。
if(length == 2 || length == 1)
答案 1 :(得分:1)
你想说的是(长度== 1 ||长度== 2)。你现在正在做的是说if(length == 2 OR 1)。前者涉及两个可以评估真或假的逻辑语句,后者一方面涉及逻辑语句,另一方面涉及整数。
计算机不会将长度== 2或1解释为“如果长度为一个或两个则返回true”,而是“(如果长度等于2则返回true)或(整数1)”。
答案 2 :(得分:0)
您可以执行以下操作:
if (Arrays.asList(1,2,3).contains(l.lenghth))
// code