2017-05-26 81 views
-1

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

但在这个即使名字没有得到证实它仍然要标记。

我该怎么做。

请帮忙。

回答

1

A switch声明不是while switch声明。

要允许接受另一个输入,请在while中包含switch,该字符在学生字符串无效时继续执行。
另外,你应该遵守约定:变量名应该以小写字母开头。
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; 
    } 
} 
+0

谢谢你这样做,我应该做什么来获得名称后要求标记,我的意思是我想,如果名称不正确,那么它不应该要求用户的标记。 –

+0

while语句旨在解决此问题。在呈现的代码中,虽然名称的输入无效,但执行的代码会在while中循环。当执行的代码恰好在while语句之后时,这意味着该名称是有效的。 – davidxxx

0

您应该关闭课程。 将“}”加到类尾

+0

我没有加入..sorry ..但它在代码中。但我的问题是不同的 –

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

谢谢你这么多,现在它工作:) –

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

注:这是做这样的一种方式。

+1

是啊,我试过这一个,它的工作,但与这个问题是我必须纠正它很多次,并与每个名字比较是非常困难的。 BTW感谢您的意见:) –

+0

@RishavSharma请再次参考答案我更新它以符合您的要求。 – Blasanka

+0

感谢男人:) \ –

相关问题