为什么这个java switch语句一直告诉我我的语句不是语句
public void setConstant(float inNumGrade)
{
this.yourNumberGrade = inNumGrade;
switch (this.yourLetterGrade)
{
case 'A':
this.yourNumberGrade >= 0.90;
break;
case 'B':
this.yourNumberGrade >= .8;
break;
case 'C':
this.yourNumberGrade >= .7;
break;
case 'D':
this.yourNumberGrade >= .6;// not a statement
default:
} // end switch
}
答案 0 :(得分:13)
我知道你要做什么,但我认为你是以错误的方式绕过它。你似乎想要做的是,根据开关设置“字母等级”,而不是数字等级!我认为你真正想做的是:
public void setGrades(float inNumGrade)
{
this.yourNumberGrade = inNumGrade;
if( this.yourNumberGrade >= 0.90)
this.yourLetterGrade = 'A';
else if(this.yourNumberGrade >=0.80)
this.yourLetterGrade = 'B';
else if (this.yourNumberGrade >=0.70)
this.yourLetterGrade= 'C';
else if (this.yourNumberGrade >=0.60)
this.yourLetterGrade= 'D';
else
this.yourLetterGrade= 'F';
}
您无法在Java中打开范围。如果您想使用开关执行此操作,则必须执行switch(true)
,然后执行case this.yourNumberGrade>=0.90:
正如我所料,你误解了开关的工作原理。如果你真的需要通过开关来做(如果/ else / else,如果更好),你必须这样做:
public void setGrades(float inNumGrade)
{
this.yourNumberGrade = inNumGrade;
switch(true)
{
case this.yourNumberGrade >= 0.90:
this.yourLetterGrade = 'A';
break;
case this.yourNumberGrade >=0.80:
this.yourLetterGrade = 'B';
break;
case this.yourNumberGrade >=0.70:
this.yourLetterGrade= 'C';
break;
case this.yourNumberGrade >=0.60:
this.yourLetterGrade= 'D';
break;
default:
this.yourLetterGrade= 'F';
break;
}//end switch
}
答案 1 :(得分:2)
因为this.yourNumberGrade >= .6;
不是编译器告诉你的有效语句。这将是一个有效的声明:
b = this.yourNumberGrade >= .6;
- 或 -
this.yourNumberGrade = .6;
这取决于你想要完成的任务。
答案 2 :(得分:1)
你到底想要做什么? >=
是比较NOT分配,这就是您收到错误的原因...只需在所有地方删除>
。
答案 3 :(得分:1)
switch / case结构将给定变量(switch参数)与可能值(case参数)进行比较,然后在匹配case语句和下一个break语句之间执行代码(或者,如果语言不支持fall通过,在下一个案例陈述之前)。
您要做的不是将变量与常量表达式进行比较,而是将变量与条件进行比较。 if / elseif结构可能是一种更清晰的表达方式:
if (this.yourNumberGrade >= 0.90) {
this.yourLetterGrade = 'A';
} else if (this.yourNumberGrade >= 0.80) {
this.yourLetterGrade = 'B';
} else if (this.yourNumberGrade >= 0.70) {
this.yourLetterGrade = 'C';
} else if (this.yourNumberGrade >= 0.60) {
this.yourLetterGrade = 'D';
} else { // you left the default out, but I assume this will be an F for Failed
this.yourLetterGrade = 'F';
}
如果你想缩短它,你可以尝试尝试使用三元运算符:
this.yourLetterGrade = (
this.yourNumberGrade >= 0.90 ? 'A' : (
this.yourNumberGrade >= 0.80 ? 'B' : (
this.yourNumberGrade >= 0.70 ? 'C' : (
this.yourNumberGrade >= 0.60 ? 'D' : 'F'
)
)
)
)
正如您所看到的,这会让您失去很多可读性,因此如果/ else可能是最干净的方法。
Eric试图向您展示的是这样的结构:
switch (true) { // We compare the boolean constant "true" to the case arguments
case this.yourNumberGrade >= 0.90:
// this is a boolean expression and evaluates either
// to "true" (matches the switch argument) or
// to "false" (does not match the switch argument)
this.yourLetterGrade = 'A';
break;
case this.yourNumberGrade >= 0.80:
this.yourLetterGrade = 'B';
break;
case this.yourNumberGrade >= 0.70:
this.yourLetterGrade = 'C';
break;
case this.yourNumberGrade >= 0.90:
this.yourLetterGrade = 'D';
break;
default:
// This is executed if none of the case arguments evaluate
// to the value of the switch argument.
this.yourLetterGrade = 'F';
// No break needed, because the end of the switch structure follows:
}
我希望能为你解决这个问题。您可能需要更加关注您尝试使用的结构的确切语义。这些结构在大多数语言中非常相似。
对于踢腿和咯咯笑,你甚至可以用数组来做:
// Our letter grades in ascending order (from bad to good).
String letterGrades[] = {'F','D','C','B','A'};
// Our number grade is in the range [0.0;1.0]. As floating point numbers are
// too precise for indexes, we want to round them down to the cut-off
// (0.9, 0.8, etc) and turn them into integer values we can use as array indices.
int gradeIndex = (int) Math.floor(this.yourNumberGrade*10);
// The lowest cut-off is 0.6, so we can treat everything lower than that the same
gradeindex = gradeindex - 5;
gradeIndex = Math.max(gradeIndex, 0);
// With Math.max we have ensured that no index can be lower than 0, now we need
// to make sure that no index is larger than the largest index in our array
// (which by definition is equal to the array's length (i.e. number of elements)
// minus 1 (because the lowest index is 0, an array of e.g. size 4 has the
// indices 0,1,2,3, but lacks an index 4 -- better get used to it, that's how
// programmers count, too).
gradeIndex = Math.min(gradeIndex, letterGrades.length-1);
// Now that our index is clean and guaranteed to be within range, we can use it
// to look up the letter grade:
this.yourLetterGrade = letterGrades[gradeIndex];
没有评论和一些简介,这甚至更短:
// Grades are as follows: A: 90%+, B: 80%+, C: 70%+, D: 60%+, F: <60%
String letterGrades[] = {'F','D','C','B','A'};
int gradeIndex = Math.min(
Math.max(0, (int) Math.floor(this.yourNumberGrade*10) - 5),
letterGrades.length-1
);
this.yourLetterGrade = letterGrades[gradeIndex];
请注意,这不太清楚,其中字母等级的确切截止点是,这就是它需要注释的原因。此外,如果截止因任何原因发生变化(例如A:85%+或F:<66.6%),您将遇到问题。您仍然可以调整计算(Math.floor(this.yourNumberGrade*10)-5
部分),但这会使其更难以遵循,如果成绩不仅仅是渐进的话,也无济于事。然而,对于传统系统来说,这是一种快速简便的方法。
答案 4 :(得分:0)
您只是在这种情况下进行的比较不是有效的陈述。
你可能意味着要做作业
答案 5 :(得分:0)
将“&gt; =”替换为“=”,如果这是您想要完成的事情。
答案 6 :(得分:0)
问题是您正在进行比较而不是分配值。也许你可以这样做:
public void setConstant(float inNumGrade)
{
this.yourNumberGrade = inNumGrade;
switch (this.yourLetterGrade)
{
case 'A':
this.yourNumberGrade = 0.90;
break;
case 'B':
this.yourNumberGrade = .8;
break;
case 'C':
this.yourNumberGrade = .7;
break;
case 'D':
this.yourNumberGrade = .6;
default:
} // end switch
}
这实际上会将值分配给“yourNumberGrade。但这只是等级的下限。最好将”yourNumberGrade“替换为”yourLetterGrade“,并确定字母等级根据数字等级......
答案 7 :(得分:0)
你必须添加休息时间;在每个案件块的内部。
switch(this.grade){
case 'A':
System.out.println("You got an A");
break;
default:
System.out.println("INVALID GRADE");
break;}