在我的应用程序中,用户必须输入控制台名称,用户名和密码。稍后程序应检查登录到动态数组的参与,如果没有,则添加它,如果是,则显示错误警告。 当你跑步时,一个输入框,静静地输入上面的名字","用户名"和#34;密码"。但是,程序不会对数组中的参与进行登录检查并显示错误消息,并且它不会自带数组字段。
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
public class User {
public static void main(String[] args) throws IOException {
BufferedReader bReader =new BufferedReader(new InputStreamReader(System.in));
String name = bReader.readLine();
String login = bReader.readLine();
String password = bReader.readLine();
UserADD(name,login,password);
}
public static ArrayList<String> UserADD(String name,String login,String password) {
ArrayList <String> users = new ArrayList<String>();
for (int i = 0; i<users.size(); i++) {
if (users.contains(login)) {
System.out.println("Это имя пользователя уже занято");
}
else users.add(name);
users.add(login);
users.add(password);
System.out.println(users);
}
return users;
}
}
答案 0 :(得分:0)
您的SELECT
st.title,
count(q.id) AS question_count,
sum(
CASE
WHEN a.answer IS NOT NULL THEN
1
ELSE
0
END
) AS answer_count,
g.user_id AS game_user_id,
a.maxid,
a.game_id AS a_game_id,
modifiedAnswer.modified AS finished,
FROM
games g
LEFT JOIN (SELECT MAX(answers.id) AS maxid, game_id FROM answers GROUP BY answers.game_id) AS a ON (a.game_id = g.id)
LEFT JOIN answers AS modifiedAnswer ON modifiedAnswer.id = a.maxid
LEFT JOIN questions q ON (a.question_id = q.id)
LEFT JOIN sessions s ON (s.id = q.session_id)
LEFT JOIN sessiontypes st ON (st.id = s.sessiontype_id)
WHERE
g.user_id = 21
GROUP BY
g.id
HAVING
(
question_count = answer_count
)
ORDER BY
finished DESC;
是本地方法。这意味着每次调用方法时,都会创建另一个对象,并且它最初是空的。
可能的解决方案是在ArrayList<String> users
方法中创建ArrayList
并将其作为参数传递给main
。
在您学习userAdd()
时,请尝试遵循最佳做法。不要使用大写字母启动函数名称。它们是为类名保留的。
答案 1 :(得分:0)
ArrayList users
在UserADD
方法下是本地的。
将ArrayList users
声明为全局将解决问题。
public class User {
//Global declaration of ArrayList users
public static ArrayList <String> users = new ArrayList<String>();
public static void main(String[] args) throws IOException {
BufferedReader bReader =new BufferedReader(new InputStreamReader(System.in));
String name = bReader.readLine();
String login = bReader.readLine();
String password = bReader.readLine();
UserADD(name,login,password);
}
public static ArrayList<String> UserADD(String name,String login,String password) {
//don't initialize users ArrayList here
for (int i = 0; i<users.size(); i++) {
if (users.contains(login)) {
System.out.println("Это имя пользователя уже занято");
}
else users.add(name); users.add (login);users.add(password);
System.out.println(users);
}
return users;
}
}
答案 2 :(得分:0)
您必须让类对象中的用户比函数调用更长寿。有点像:
public class UserRegistration {
private List<String> users = new ArrayList<>();
public static void main(String[] args) throws IOException {
new UserRegistration().run();
}
private void run() {
BufferedReader bReader =new BufferedReader(new InputStreamReader(System.in));
String name = bReader.readLine();
String login = bReader.readLine();
String password = bReader.readLine();
addUser(name,login,password);
}
public boolean addUser(String name,String login,String password) {
for (int i = 0; i<users.size(); i++) {
if (users.contains("\t" + login + "\t")) {
System.out.println("Это имя пользователя уже занято");
return false;
}
}
users.add(name + "\t" + login + "\t" + password);
return true;
}
}
答案 3 :(得分:0)
最佳解决方案是
public class User {
Set<UserLogin> users = new HashSet<UserLogin>();
public static void main(String[] arg) throws IOException {
BufferedReader bReader =new BufferedReader(new InputStreamReader(System.in));
String name = bReader.readLine();
String login = bReader.readLine();
String password = bReader.readLine();
new User().UserADD(new User(). new UserLogin(name, login, password));
}
private void UserADD(final UserLogin user) {
if (!users.contains(user)) {
users.add(user);
} else {
System.out.println("Это имя пользователя уже занято");
}
}
public class UserLogin {
private String name;
private String login;
private String pass;
public UserLogin(String name, String login, String pass) {
this.name = name;
this.login = login;
this.pass = pass;
}
public String getName() {
return name;
}
public String getLogin() {
return login;
}
public String getPass() {
return pass;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
UserLogin user = (UserLogin) o;
return !(login != null ? !login.equals(user.login) : user.login != null);
}
@Override
public int hashCode() {
return login != null ? login.hashCode() : 0;
}
}
}
答案 4 :(得分:0)
非常感谢你,但问题仍然存在。当您尝试第二个用户首先输入用户名时,程序会平静地接受它,忽略任何限制并且屏幕上的警告不会出现。我尝试了所有的选择。