本问题已经有最佳答案,请猛点这里访问。
这是我的代码:
public class Main extends JFrame{
static int NoOfUsers;
static String[][] Accounts = new String[NoOfUsers][2];
public static void main(String[] args){
Login();
}
private static void Login() {
final String FileName ="F:/TextFiles/loginaccs.txt";
try {
BufferedReader file = new BufferedReader(new InputStreamReader(new FileInputStream(FileName)));
int NoOfUsersL = Integer.parseInt(file.readLine());
String[][] Accounts = new String[NoOfUsersL][2];
NoOfUsers = NoOfUsersL;
for (int i=0; i
Accounts[i][0] = file.readLine();
Accounts[i][1] = file.readLine();
}
for (int i=0; i
System.out.println(Accounts[i][0]);
System.out.println(Accounts[i][1]);
}
file.close();
} catch (IOException e) {
System.out.println("ERROR: unable to read file.");
e.printStackTrace();
}
String username = null;
String password = null;
JTextField usernamejtf = new JTextField(username);
JPasswordField passwordjtf = new JPasswordField(password);
String[] buttons = {"Login","Create new account"};
Object[] InputDialog = {
"Username:", usernamejtf,"Password:", passwordjtf
};
do {
int option = JOptionPane.showOptionDialog(null,
InputDialog,
"Login",
JOptionPane.DEFAULT_OPTION,
JOptionPane.PLAIN_MESSAGE,
null,
buttons,
buttons[0]);
System.out.println(option); //Check
if (option == JOptionPane.CLOSED_OPTION ) {
return;
}
else if (option == 0) {
if (CheckAccount(username,password)) {
System.out.println("Logged in");
} else {
System.out.println("Wrong Password/Username");
}
} else if (option == 1) {
System.out.println("Create Account.");
}
} while (!(CheckAccount(username,password)));
}
private static boolean CheckAccount(String username, String password) {
for (int i=0; i>NoOfUsers; i++) {
if ((username == Accounts[i][0]) && (password == Accounts[i][1])) {
return true;
}
}
return false;
}
}
在"main"中,我调用了login()方法,Eclipse强迫我将单词"static"放在方法名前面。我是否可以修改程序,使行可以写为:private void login()…私有布尔支票帐户(…)…等?
[附加问题:因为"static"这个词,我不能把"public"这个词放在string[]accounts=new string[noolfusersl][2]之前;这使得checkaccount无法访问accounts数组。如何修改程序以解决此问题。]提前通知大家。
您必须创建一个EDOCX1的实例(0),以便将方法用作实例方法。
它强迫您使用static,因为非静态方法属于instance,而不是class。您必须创建一个Main的实例。
回答额外的问题:可以。静态变量也被称为类变量。局部变量不能声明为静态。
静态方法静态方法是呼叫(不知道安安审审法)的呼叫建立和呼叫从第一页第一类为as belowP></
public static void main(String[] args){
Main main = new Main();
main.login();
}
private void login() { //remove static from instance methods
哇,不知道这么简单,太多了。
为什么要将主实例存储在变量中?你浪费了记忆。
@b&lint内存与new Main()一起使用。把它保存在变量中并不像你说的那样重要。这个变量只是对它的引用。
@很抱歉,不客气。不要忘记调用setVisible方法来显示jframe,还需要使用setBounds来设置帧的大小和位置。
在静态qualifiers阶to remove方法,你必须让你的布尔变量的方法和实例变量。然后,你可以和你的呼叫login()JFrame对象实例化。P></
转换的方法也mixedcase /变量名称As to Java offical公约。恩,你把我# VB或C语言编程?P></
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;
public class Main extends JFrame {
private static final long serialVersionUID = -105943237549003486L;
private int numOfUsers;
private String[][] accounts;
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
Main m = new Main();
m.login("F:/TextFiles/loginaccs.txt");
}
});
}
// Constructor, set up instance values here.
public Main() {
super();
}
private void login(final String fileName) {
try {
BufferedReader file = new BufferedReader(new InputStreamReader(new FileInputStream(fileName)));
numOfUsers = Integer.parseInt(file.readLine());
accounts = new String[numOfUsers][2];
for (int i = 0; i accounts[i][0] = file.readLine();
accounts[i][1] = file.readLine();
}
for (int i = 0; i System.out.println(accounts[i][0]);
System.out.println(accounts[i][1]);
}
file.close();
} catch (IOException e) {
System.out.println("ERROR: unable to read file.");
e.printStackTrace();
}
String username = null;
String password = null;
JTextField usernamejtf = new JTextField(username);
JPasswordField passwordjtf = new JPasswordField(password);
String[] buttons = {"Login","Create new account" };
Object[] InputDialog = {"Username:", usernamejtf,"Password:", passwordjtf };
do {
int option = JOptionPane.showOptionDialog(null, InputDialog,"Login", JOptionPane.DEFAULT_OPTION,
JOptionPane.PLAIN_MESSAGE, null, buttons, buttons[0]);
System.out.println(option); // Check
if (option == JOptionPane.CLOSED_OPTION) {
return;
} else if (option == 0) {
if (checkAccount(username, password)) {
System.out.println("Logged in");
} else {
System.out.println("Wrong Password/Username");
}
} else if (option == 1) {
System.out.println("Create Account.");
}
} while (!(checkAccount(username, password)));
}
private boolean checkAccount(String username, String password) {
for (int i = 0; i > numOfUsers; i++) {
if (username == accounts[i][0] && password == accounts[i][1]) {
return true;
}
}
return false;
}
}
使用:P></
public static void main(String[] args){
Main main = new Main();
main.login();
}
为什么需要将实例存储在变量中?你浪费了记忆。
@B&lint它无论如何都将存储在内存中,因为它是在启动线程中执行的。
给new Main().login();P></
You should naming约定后续变量和Java method should start,lowercase名称与字母。P></