2017-05-26 92 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

感謝男人:) \ –

相關問題