2016-11-28 6 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を使用することができ、ユーザを認証するために2つの方法のいずれかを適用する/使用している場合:

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

EDIT

authenticate2にコンパイルエラーを取り除くには、そのauthenticate2をメインクラスに追加するか、メソッド呼び出しにクラス名を含めます。

if (authenticate2.authenticate2(input))

変更

if (authenticate2(input)) {

これは最初のものは成功し、その逆であれば制御は二ifブロックに入らないことを確認します。

+0

認証クラスのフェッチャーでエラーが発生します。正方形1に戻ってください。認証2でまだエラーがあります。 – chaosillusion

+0

私はコードを更新しました。今は、authentication2のパスワードがどこにあるのかという問題があります。どのように私はこの作品を作るのですか?メインストリングにはもうエラーはありません。 – chaosillusion

+0

私は答えを更新しました。 –

関連する問題