【问题标题】:When i try to use method it give error [closed]当我尝试使用方法时它给出错误[关闭]
【发布时间】:2017-05-26 17:46:35
【问题描述】:

我有一个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");
        }
    }
}

但在这方面,即使名称没有得到验证,它仍然要求标记。

我该怎么办。

请帮忙。

【问题讨论】:

    标签: java if-statement methods switch-statement goto


    【解决方案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;
        }
    }
    

    【讨论】:

    • 谢谢你,我应该怎么做才能在得到名字后要求标记,我的意思是我希望如果名字不正确,那么它不应该要求用户标记。
    • while 语句就是为了解决这个问题而设计的。在呈现的代码中,虽然名称的输入无效,但执行的代码会在 while 中循环。当执行的代码恰好在while语句之后,说明名字有效。
    【解决方案2】:

    你应该下课了。 在类的末尾添加“}”

    【讨论】:

    • 我没有添加那个..对不起..但它在代码中。但我的问题不同
    【解决方案3】:

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

    注意:这是一种方法。

    【讨论】:

    • 是的,我试过这个并且它有效,但问题是我必须多次纠正它,并且与每个名称进行比较非常困难。顺便说一句,感谢您的评论:)
    • @RishavSharma 请再次参考答案我更新它以满足您的要求。
    猜你喜欢
    • 2018-06-30
    • 1970-01-01
    • 1970-01-01
    • 2022-01-11
    • 1970-01-01
    • 2015-08-08
    • 2021-03-04
    • 1970-01-01
    • 2013-12-04
    相关资源
    最近更新 更多