正如问题的标题一样。我遇到问题的地方是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 :(得分: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
块,如果第一个成功,反之亦然。