我有一个switch
声明,其中我试图检查string
值,我希望在检查名称后,它应该要求标记。但是当我尝试在方法中使if else
语句出错时。当我尝试使用方法,它会给出错误
这里是一个运行良好的代码:
import java.util.Scanner;
public class java {
public static void main(String args[]) {
Scanner obj = new Scanner(System.in);
int num;
final int pass = 33;
String Student;
System.out.println("Please enter your name");
Student = obj.nextLine();
switch (Student) {
case "Ram":
System.out.println("Students name is " + Student);
break;
case "Shyaam":
System.out.println("Students name is " + Student);
break;
default:
System.out.println("This name is not recognised");
}
System.out.println("Enter your marks");
num = obj.nextInt();
if (num < pass) {
System.out.println(Student + " You have failed the subject");
} else {
System.out.println(Student + " You have passed the subject");
}
}
}
但在这个即使名字没有得到证实它仍然要标记。
我该怎么做。
请帮忙。
谢谢你这样做,我应该做什么来获得名称后要求标记,我的意思是我想,如果名称不正确,那么它不应该要求用户的标记。 –
while语句旨在解决此问题。在呈现的代码中,虽然名称的输入无效,但执行的代码会在while中循环。当执行的代码恰好在while语句之后时,这意味着该名称是有效的。 – davidxxx