2017-02-09 12 views
-2

コードは正常に機能しますが、間違ったユーザー名を入力すると出力がなくなります。このコードのヒントやアドバイスはありますか?私はif... elseを使うことができると知っていますが、私はswitchでこれをやろうとしています。スイッチを使用するパスワード/ユーザー名

package frame.security; 
import java.util.Scanner; 

public class FrameIntel { 
    public static void main(String[] args) { 

     Scanner sc=new Scanner(System.in); 
     String Username; 
     System.out.println("Enter Username:"); 
     Username=sc.nextLine(); 
     String Password; 
     System.out.println("Enter Password: "); 
     Password=sc.nextLine(); 

     switch(Username) { 
     case"Winford Coloma": 
      if("TwelveEleven".equals(Password)) { 
       System.out.println("Senior Software Engineer"); 
       System.err.println("Limited Access to Sandbox!(4)"); 
       break; 
      } 
      else { 
       System.err.println("Access Denied!"); 
       System.exit(0); 
      } 
     case"Lynelle Marten": 
      if("TwelveSixteen".equals(Password)) { 
       System.out.println("Test Administrator"); 
       System.err.println("Limited Access to Sandbox!(5)"); 
       break; 
      } 
      else { 
       System.err.println("Access Denied"); 
       System.exit(0); 
      } 
     case"Luis Ansley": 
      if("TwelveTwenty".equals(Password)) { 
       System.out.println("Software Engineer"); 
       System.err.println("Controlled Access to Sandbox!(Clog)"); 
       break; 
      } 
      else { 
       System.err.println("Access Denied"); 
       System.exit(0); 
      } 
     case"Shantay Dority": 
      if("TwelveTwentyFour".equals(Password)) { 
       System.out.println("Programmer"); 
       System.err.println("Implement only(Sandbox)!"); 
       break; 
      } 
      else { 
       System.err.println("Access Denied"); 
       System.exit(0); 
      } 
     case"Tangela Norsworthy": 
      if("TwelveTwentySeven".equals(Password)){ 
       System.out.println("CEO"); 
       System.err.println("Full access to Sandbox!"); 
       break; 
      } 
      else { 
       System.err.println("Access Denied"); 
       System.exit(0); 
      } 


     } 

    } 
} 
+0

ここでは「デフォルト」が必要です。 –

+0

['default'セクションは、' case'セクションの1つによって明示的に処理されないすべての値を処理します。](https://docs.oracle.com/javase/tutorial/java/nutsandbolts/switch.html) – alex

答えて

0

コードが唯一あなたがデフォルトケースなしスイッチ文を使用しているあなたのcase

0

以内に書かれたユーザ名で動作しますので、あなたは、default文はありません。 デフォルトの大文字小文字は、「大文字小文字」のヒットが一致しない場合に使用されます。

があなたのケース"Tangela Norsworthy"後にこれらの行を追加します。 "System.exit();" で

default: { 
    System.err.println("Username not recognised"); 
    System.exit(0); 
} 

0を示し、そのアプリケーションは問題なく終了しました。何か問題があったことを示すために他の値を使うべきです。 (つまりSystem.exit(1) - > 1 =無効なユーザー名、System.exit(2) - > 2 =無効なパスワード)

+0

あなたは、今それを持っています。 – Silvervud1996

関連する問題