Java 当我尝试使用这个方法时,它给出了错误

Java 当我尝试使用这个方法时,它给出了错误,java,if-statement,methods,switch-statement,goto,Java,If Statement,Methods,Switch Statement,Goto,我有一个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;

我有一个
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");
        }
    }
}
import java.util.Scanner;
公共类java{
公共静态void main(字符串参数[]){
扫描器obj=新扫描器(System.in);
int-num;
最终积分通过=33;
弦乐学生;
System.out.println(“请输入您的姓名”);
Student=obj.nextLine();
开关(学生){
案例“Ram”:
System.out.println(“学生名为”+学生名);
打破
“Shyaam”案:
System.out.println(“学生名为”+学生名);
打破
违约:
System.out.println(“此名称不可识别”);
}
System.out.println(“输入您的分数”);
num=obj.nextInt();
if(num
但在这种情况下,即使名字没有得到验证,它仍然要求标记

我该怎么办

请帮忙。

你应该停课。
在类的末尾添加“}”一个
开关
语句不是
while开关
语句。

要允许进行另一个输入,请在学生字符串无效时将
开关
包含在
while
中。
此外,您应该遵守约定:变量名应该以小写字母开头。
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;
    }
}

您可以将
条件移动到
开关内部的

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");
    }

注意:这是一种方法。

我没有添加这一点。。抱歉。。但它在代码中。但是我的问题是不同的,在得到名字后,我应该怎么做才能要求分数,我的意思是,如果名字不正确,那么它就不应该向用户要求分数。while语句就是为了解决这个问题而设计的。在呈现的代码中,虽然名称的输入无效,但执行的代码在while中循环。当执行的代码恰好在while语句之后时,这意味着名称是有效的。是的,我尝试了这个方法,它成功了,但问题是我必须多次更正它,并且与每个名称进行比较非常困难。顺便说一句,谢谢你的评论:)@RishavSharma请再次参考答案,我更新了它以符合你的要求。
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 ....");
   }
}