我有一个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");
}
}
}
但在这种情况下,即使名称没有得到验证,它仍然要求标记。
我应该怎么办。
请帮忙。
一个switch
说法是不是一个while switch
声明。
为了允许接受另一个输入,包括switch
在while
学生字符串无效时继续的输入。
此外,您应该遵守约定:变量名称应以小写开头。
String student
是正确的。String Student
不是。
String student = null;
while (student == null){
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");
student = null;
}
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句