当我尝试使用方法时,它会给出错误

时间:2017-05-26 17:46:35

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

但是即使名称没有得到验证,它仍然会要求标记。

我应该怎么做。

请帮忙。

4 个答案:

答案 0 :(得分:1)

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

答案 1 :(得分:0)

你应该关闭课程。 将“}”添加到类的末尾

答案 2 :(得分:0)

您可以使用boolean检查名称是否正确,例如:

boolean checkName = true;//<<---------------create a boolean
switch (Student) {
    case "Ram":
        System.out.println("Students name is " + Student);
        break;
    case "Shyaam":
        System.out.println("Students name is " + Student);
        break;
    default:
        checkName = false;//<<---------------if the name is not correct, assign false
        System.out.println("This name is not recognised");

}

if (checkName) {//<<-------check if your name is correct, then you can ask the marks
    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");
    }
}

答案 3 :(得分:0)

您可以将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 ....");
   }
}

注意:这是一种方法。