gpt4 book ai didi

java - 如何解决 addFriends 方法中的问题

转载 作者:行者123 更新时间:2023-12-02 01:22:27 25 4
gpt4 key购买 nike

我举了一个例子来解释我的问题我使用用户“Clarke”登录,并向用户“John”发送好友请求在我从克拉克的帐户注销并使用“约翰”登录后。我选择案例 -> 4 并向我显示来自 Clarke 的好友请求,但是当我写下 Clarke 姓名以接受请求时,程序不会进入 addFriends 方法。

用户类别:

 import java.util.*;
import java.sql.*;

public class User
{

private int like=0;
private String username;
private String email;
Scanner scanner = new Scanner(System.in);


public User (String username , String email){
this.username=username;
this.email=email;
}
ArrayList <User> friends= new ArrayList<>();
ArrayList <User> friendrequest = new ArrayList<>();
public void addFR(User u){
friendrequest.add(u);
System.out.println(" a request has been sent to this user");
}
public void readFR(){
for(int p=0 ;p< friendrequest.size() ;p++){
System.out.println(friendrequest.get(p).getUsername());
}

}
public void sendFR(){
System.out.println("Search for a friend");
}
public void removeFriends(User u){
friends.remove(u);
}
public void addFriends(User u){

if(!friends.equals(u) && !username.equals(u.getUsername()) &&
friendrequest.equals(u) ){

System.out.println("You have a friend request!");
System.out.print("If you want to accept it write 1:") ;
int a= scanner.nextInt();
if(a==1){
friends.add(u);
System.out.println("You have a new friend!");
}


}


}


public Timestamp Time(){
Timestamp timestamp = new Timestamp(System.currentTimeMillis());
return timestamp;
}
public void like(Message m){
like++;
System.out.println("Likes of this message are: " + like);

}
public void yourFriends(){
System.out.println("Your Friends:");
for(int y= 0; y< friends.size(); y++){
System.out.println(friends.get(y).getUsername());
}
}
public void returnFriends(){


for(Object value: friends){
System.out.println("Your Friends Are: "+ value);

}
}

public String getUsername() {
return username;
}
public String getEmail(){
return email;
}
}

主类:

   import java.util.*;

public class Main
{
public static void main(String args[]){
Scanner scan= new Scanner(System.in);
User cg = new User("Clarke" ,"griffinc@gmail.com");
User bb = new User("Bellamy" ,"blakeb@gmail.com");
User ob = new User("Octavia" ,"blakeo@gmail.com");
User rr = new User("Raven" ,"reyesr@gmail.com");
User jm = new User("John" ,"murphyj@gmail.com");
User mg = new User("Monty" ,"greenm@gmail.com");

Message message1= new Message("Clarke","Clake :...");
Message message2= new Message("Bellame","Bellame:");
Message message3= new Message("Octavia","Octavia:..");
Message message4= new Message("Raven","Raven:..");
Message message5= new Message("John","John:...");
Message message6= new Message("Monty","Monty:Be the good guys.May
we met again");

ArrayList <User> users= new ArrayList<>();
ArrayList<Message> message= new ArrayList<>();


users.add(cg);
users.add(bb);
users.add(ob);
users.add(rr);
users.add(jm);
users.add(mg);

message.add(message1);
message.add(message2);
message.add(message3);
message.add(message4);
message.add(message5);
message.add(message6);



//login
boolean login=false;
boolean newlogin=false;
User loginUser=new User(" "," ");
while(newlogin==false){
while (login==false){
for(int j=0; j<users.size(); j++)
{
System.out.println(j+1 +":"+ users.get(j).getUsername());
}
System.out.println("Give your username:");
String username=scan.next();
for(int i = 0; i < users.size(); i++)
{
if(username.equals(users.get(i).getUsername() ) ) {
loginUser=users.get(i);
login=true;

}

}


}

//Menu
boolean luna=false;
System.out.println("You are online!");
while(luna==false){
System.out.println("||Menu|| Choose:");
System.out.println("1 -> See Your Wall");
System.out.println("2-> See Your Friend Wall");
System.out.println("3-> Send friend request");
System.out.println("4->Accept/Reject friend request");
System.out.println("5->See My Friends");
System.out.println("6->");
System.out.println("7->New Login");
System.out.println("8->Exit");
int choice =scan.nextInt();

switch(choice){
case 1:
for(int z = 0; z < message.size(); z++){
if(loginUser.getUsername().equals(message.get(z).getUsername() ))
{
System.out.println("//Your Wall//" + message.get(z).getPost() +
message.get(z).Time());
}
}
luna=false;
break;
case 2:
System.out.println("Who's wall do you want to see");
String myf=scan.next();
for(int w = 0; w< users.size(); w++){
if(myf.equals(users.get(w).getUsername()) ){
System.out.print( "//" + myf + "'s Wall//" );
for(int q = 0; q < message.size(); q++){
if (myf.equals(message.get(q).getUsername())){
System.out.println(message.get(q).getPost());
}
}
}
}
luna=false;
break;

case 3:
loginUser.sendFR();
String fr= scan.next();
for(int p = 0; p< users.size(); p++){
if(fr.equals(users.get(p).getUsername()) &&
!fr.equals(loginUser.getUsername())){
users.get(p).addFR(loginUser);
}

}
luna =false;
break;

case 4:
loginUser.readFR();
System.out.println("Do you want someone for be your friend?(write
his username)");
String yn=scan.next();
for(int t = 0; t< users.size(); t++)
{
if(yn.equals(users.get(t).getUsername()))
loginUser.addFriends(users.get(t));
}
luna =false;
break;
case 5:
loginUser.yourFriends();
luna=false;
break;
case 7:
luna=true;
login=false;
break;
case 8:
luna=true;
newlogin=true;
break;
}
}
}
}
}

最佳答案

您的问题是在 User 类的 addFriends() 方法中的这一行:

if(!friends.equals(u) && !username.equals(u.getUsername()) && friendrequest.equals(u) )

在与 friendsfriendrequests 进行比较时,应该使用 .contains() 而不是 .equals,因为您当前正在将 ListUser 进行比较。

它应该是这样的:

if(!friends.contains(u) && !username.equals(u.getUsername()) && friendrequest.contains(u) 

我使用此修复测试了您的代码,它能够成功添加 friend 。

关于java - 如何解决 addFriends 方法中的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57612570/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com