2016-11-28 15 views
-2

這就像問題題目一樣。我遇到問題的區域是if/else屬性..還有,我將如何將其從API接口更改爲數組類型接口?對不起,如果我的格式很差。隨時糾正我。如何添加更多帳戶和輸出到我的密碼驗證程序?

程序糾正並運行它是如何打算:

import java.io.File; 
import java.io.FileNotFoundException; 
import java.io.FileOutputStream; 
import java.io.PrintStream; 
import java.util.Scanner; // Needed for the Scanner class 

public class PasswordVerifier2 { 

public static void main(String[] args) throws FileNotFoundException { 

    Scanner keyboard = new Scanner(System.in); 
    String input; 

    System.out.print("Please enter your password: "); 
    input = keyboard.nextLine(); 

if (authenticate1(input)) { 

     System.out.println("This program is working if this text is found within outputfile.txt."); 

     File file = new File("outputfile.txt"); 
     FileOutputStream fos = new FileOutputStream(file); 
     PrintStream ps = new PrintStream(fos); 
     System.setOut(ps); 
     System.out.println("This program is working if this text is found within outputfile.txt."); 

}else if (authenticate2(input)) { 

     System.out.println("It works."); 

}else{ 
System.out.println("Error: Wrong password."); 
} 
} 

private static boolean authenticate1(String password1) { 

    return ((password1.length() == 6) 
      && (password1.matches("beep11")) 
      && (password1.matches("beep11")) 
      && (password1.matches("beep11"))); 
} 

private static boolean authenticate2(String password2) { 

      return ((password2.length() == 6) 
      && (password2.matches("beep22")) 
      && (password2.matches("beep22")) 
      && (password2.matches("beep22"))); 
} 
} 

回答

0

如果使用/應用兩種方法之一來驗證用戶,那麼你可以使用if.. else if,如:

if (authenticate1(input)) { 
// some code 
}else if (authenticate2(input)) { 
//some code 
}else{ 
System.out.println("Error: Wrong password."); 
} 

編輯

要擺脫編譯錯誤authenticate2,您需要移動將authenticate2添加到您的主類,或者在方法調用中包含類名

變化

if (authenticate2(input)) {

if (authenticate2.authenticate2(input))

這將確保該控件不進入第二if塊,如果第一個是成功的,反之亦然。

+0

這將無法正常工作,並且我對驗證類fetchers有錯誤。回到原點1.在身份驗證2處仍然存在此錯誤。 – chaosillusion

+0

我更新了代碼。現在我遇到了authentication2密碼所在的問題。我如何完成這項工作?主字符串中不再有錯誤。 – chaosillusion

+0

我已經更新了答案。 –