当我尝试使用方法时出现错误
When i try to use method it give error
我有一个 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");
}
}
}
但在此即使名称未通过验证,它仍然要求标记。
我该怎么办
请帮忙。
您应该关闭 class。
class
末尾加“}”
switch
语句不是 while switch
语句。
要允许接受另一个输入,请将 switch
包含在 while
中,该 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;
}
}
您可以将 if
条件移动到 switch
内部。
switch(Student){
case "Ram":
System.out.println("Students name is "+ Student);
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");
}
break;
case "Shyaam":
System.out.println("Students name is "+ Student);
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");
}
break;
default:
System.out.println("This name is not recognised");
}
但这会导致您使用更多代码来避免您可以使用更多变量来检查学生姓名是否匹配。为此,您必须使用 boolean
变量。
那么代码应该是:
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();
boolean isStudent = false;
switch(Student){
case "Ram":
isStudent = true;
System.out.println("Students name is "+ Student);
break;
case "Shyaam":
isStudent = true;
System.out.println("Students name is "+ Student);
break;
default:
System.out.println("This name is not recognised");
}
if(isStudent){
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");
}
}else{
System.out.println("You are not a ....");
}
}
注意:这是执行此操作的一种方法。
我有一个 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");
}
}
}
但在此即使名称未通过验证,它仍然要求标记。
我该怎么办
请帮忙。
您应该关闭 class。 class
末尾加“}”switch
语句不是 while switch
语句。
要允许接受另一个输入,请将 switch
包含在 while
中,该 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;
}
}
您可以将 if
条件移动到 switch
内部。
switch(Student){
case "Ram":
System.out.println("Students name is "+ Student);
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");
}
break;
case "Shyaam":
System.out.println("Students name is "+ Student);
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");
}
break;
default:
System.out.println("This name is not recognised");
}
但这会导致您使用更多代码来避免您可以使用更多变量来检查学生姓名是否匹配。为此,您必须使用 boolean
变量。
那么代码应该是:
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();
boolean isStudent = false;
switch(Student){
case "Ram":
isStudent = true;
System.out.println("Students name is "+ Student);
break;
case "Shyaam":
isStudent = true;
System.out.println("Students name is "+ Student);
break;
default:
System.out.println("This name is not recognised");
}
if(isStudent){
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");
}
}else{
System.out.println("You are not a ....");
}
}
注意:这是执行此操作的一种方法。