我需要一些帮助,我在互联网上找到了这个代码,这里是代码:
import java.util.Scanner;
class login {
public static void main(String args[]){
Scanner luci = new Scanner(System.in);
String idInput;
System.out.println("Username: ");
idInput = luci.nextLine();
String passwordInput;
System.out.print("Password: ");
passwordInput = luci.nextLine();
if(passwordInput.equals("PASSWORD_GOES_HERE") && (idInput.equals("USERNAME_GOES_HERE"))){
System.out.println("Authentification complete!");
} else {
System.out.println("Wrong ID or Password!");
}
}
}
当我在Netbeans中运行此代码时,它工作得很好,但是有一个问题,它依赖于控制台,你需要在控制台中给它提供登录信息。有什么方法我可以让这个代码在外面运行Netbeans?
答案 0 :(得分:1)
您可以将结果存储在String变量中并将其写入文件,而不是将其打印到控制台,如下所示:
String s;
if (passwordInput.equals("PASSWORD_GOES_HERE")
&& (idInput.equals("USERNAME_GOES_HERE"))) {
s="Authentification complete!";
} else {
s="Wrong ID or Password!";
}
try {
JFileChooser chooser = new JFileChooser("Select file name");
int option = chooser.showSaveDialog(foo);
if (option == JFileChooser.APPROVE_OPTION) {
new PrintStream(new FileOutputStream(chooser.getSelectedFile().getCanonicalPath())).print(s);
}
} catch (Exception e) {
e.printStackTrace();
}