我们的老师告诉我们关于if-else
声明,他正在教我们如何switch
声明可以替代if-else
所以我编码,我得到这个无法访问的声明错误。我不知道我是如何调试因为我刚刚将我的Java从6更新到了8,而且我真的不知道我的代码有什么问题。java 8无法访问的语句开关case case
import java.io.*;
import java.util.Scanner;
class Switch
{
public static void main(String[] args)
{
Scanner k = new Scanner(System.in);
System.out.println("Whats your name?");
String name = k.nextLine();
System.out.println("Do you want to compute your grades?");
String yesNo = k.nextLine();
switch(yesNo)
{
case "yes":
System.out.println("Input your attendance grade(10%)\n");
int attendance = k.nextInt();
System.out.println("Input your participation grade (10%)\n");
int participation = k.nextInt();
System.out.println("Input your quiz grade (10%)\n");
int quiz = k.nextInt();
System.out.println("Input your exam grade(40%)\n");
int exam = k.nextInt();
System.out.println("Input your lab grade (20%)\n");
int lab = k.nextInt();
double point10 = 0.10;
double point20 = 0.20;
double point40 = 0.40;
double attendanceC = attendance*point10;
double participationC = participation*point10;
double quizC = quiz*point20;
double examC = exam*point40;
double labC = lab*point20;
double yourGradeD = attendanceC+participationC+quizC+examC+labC;
int yourGrade = (int)yourGradeD;
break;
default:
System.out.println("Thank you "+name);
break;
switch(yourGrade)
{
case 99|98|97|96|95|94|93|92|91|90:
System.out.println("testing");
break;
}
}
}
}
'情况下99 | 98 | 97 | 96 | 95 | 94 | 93 | 92 | 91 | 90:'不会做你认为它的确如此。 – Pshemo
错误是由'default:'子句中的'break'后的'switch(yourGrade)'造成的。请注意'switch(yourGrade)'可能是其他任何Java语句,并且错误仍会发生。原因是代码永远无法到达,因为你刚刚通过'break'跳出了'default:'子句。 –
如果(yourGrade> = 90 && yourGrade <= 99){系统输出(检测);} – QuakeCore