主要的问题是自建的reg方法中判断能否存在已注册的用户名的问题一直存在,重写了equals方法好像并没有效果,a.contains(userCheck)一直返回的是false。困扰了一个上午了。希望高手给看看吧。
下面是源码:
练习:使用集合实现注册登陆功能
package cn.ustb.practice;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Scanner;
class User{
String name;
String passport;
public User(String name,String passport){
this.name = name;
this.passport = passport;
}
public void setName(String name){
this.name = name;
}
public void setPassport(String passport){
this.passport = passport;
}public String getName(){
return this.name;
}public String getPasspoet(){
return this.passport;
}
@Override
public String toString() {
// TODO Auto-generated method stub
return “[用户名:”+this.name+”;密码:”+this.passport+”]”;
}
@Override
public boolean equals(Object obj) {
User user = (User) obj;
return (this.name == user.name);
}
@Override
public int hashCode() {
// TODO Auto-generated method stub
return 1;
}
}
public class Wch2 {
static Collection a = new ArrayList();
static Scanner scanner = new Scanner(System.in);
public static void main(String[] args){
while(true){
System.out.println(“请输入:(A)注册 (B)登陆”);
String option = scanner.next();
if(option.equalsIgnoreCase(“A”)){
reg();
}else if (option.equalsIgnoreCase(“B”)){
login();
}else{
System.out.println(“选项错误,重新输入:”);
}
}
}
public static void reg() {
User user;
System.out.println(“请输入要注册的用户名:”);
String userName = scanner.next();
user = new User(userName,null);
if(a.contains(user)){
System.out.println(“用户名已存在,请重新输入:”);
}else{
System.out.println(“请设置密码:”);
String passport = scanner.next();
user.setPassport(passport);
a.add(user);
System.out.println(“设置完毕,您设置的账户是:”);
Object[] arr = a.toArray();
User userShow = (User)arr[a.size()-1];
System.out.println(userShow);
System.out.println(a);
}
}
public static void login() {
User user2 = null;
System.out.println(“请输入要登录的用户名:”);
String userName = scanner.next();
User userCheck = new User(userName,null);
if(a.contains(userCheck)){
System.out.println(“请输入密码:”);
String passport = scanner.next();
user2.setName(userName);
user2.setPassport(passport);
if(a.contains(user2)){
System.out.println(“登陆成功”);
}else{
System.out.println(“密码错误,重新输入”);
}
}else{
System.out.println(“用户名不存在,重新输入”);
login();
}
}
}
下面是源码:
练习:使用集合实现注册登陆功能
package cn.ustb.practice;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Scanner;
class User{
String name;
String passport;
public User(String name,String passport){
this.name = name;
this.passport = passport;
}
public void setName(String name){
this.name = name;
}
public void setPassport(String passport){
this.passport = passport;
}public String getName(){
return this.name;
}public String getPasspoet(){
return this.passport;
}
@Override
public String toString() {
// TODO Auto-generated method stub
return “[用户名:”+this.name+”;密码:”+this.passport+”]”;
}
@Override
public boolean equals(Object obj) {
User user = (User) obj;
return (this.name == user.name);
}
@Override
public int hashCode() {
// TODO Auto-generated method stub
return 1;
}
}
public class Wch2 {
static Collection a = new ArrayList();
static Scanner scanner = new Scanner(System.in);
public static void main(String[] args){
while(true){
System.out.println(“请输入:(A)注册 (B)登陆”);
String option = scanner.next();
if(option.equalsIgnoreCase(“A”)){
reg();
}else if (option.equalsIgnoreCase(“B”)){
login();
}else{
System.out.println(“选项错误,重新输入:”);
}
}
}
public static void reg() {
User user;
System.out.println(“请输入要注册的用户名:”);
String userName = scanner.next();
user = new User(userName,null);
if(a.contains(user)){
System.out.println(“用户名已存在,请重新输入:”);
}else{
System.out.println(“请设置密码:”);
String passport = scanner.next();
user.setPassport(passport);
a.add(user);
System.out.println(“设置完毕,您设置的账户是:”);
Object[] arr = a.toArray();
User userShow = (User)arr[a.size()-1];
System.out.println(userShow);
System.out.println(a);
}
}
public static void login() {
User user2 = null;
System.out.println(“请输入要登录的用户名:”);
String userName = scanner.next();
User userCheck = new User(userName,null);
if(a.contains(userCheck)){
System.out.println(“请输入密码:”);
String passport = scanner.next();
user2.setName(userName);
user2.setPassport(passport);
if(a.contains(user2)){
System.out.println(“登陆成功”);
}else{
System.out.println(“密码错误,重新输入”);
}
}else{
System.out.println(“用户名不存在,重新输入”);
login();
}
}
}
解决方案
80
for (User user : a) { }
这个是增强for循环 不知道你学了没有
用普通循环也行
for(int i=0;i<a.size();i++){
}
这个是增强for循环 不知道你学了没有
用普通循环也行
for(int i=0;i<a.size();i++){
}