gpt4 book ai didi

java - 为什么我的服务器跳过 if 语句并转到 else 部分(重试)?

转载 作者:行者123 更新时间:2023-12-01 06:21:40 25 4
gpt4 key购买 nike

我的服务器代码

问题出在服务器代码中,当检查登录用户名和密码时,它每次都会输出“重试”消息

import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.*;
import java.util.ArrayList;
//test
public class server {


public static void main(String[] args) {
// TODO Auto-generated method stub

ServerSocket listener;
int clientid=0;
try
{
listener = new ServerSocket(10000,10);

while(true)
{
System.out.println("Main thread listening for incoming new connections");
Socket newconnection = listener.accept();

System.out.println("New connection received and spanning a thread");
Connecthandler t = new Connecthandler(newconnection, clientid);
clientid++;
t.start();
}

}

catch (IOException e)
{
System.out.println("Socket not opened");
// TODO Auto-generated catch block
e.printStackTrace();
}
}

}


class Connecthandler extends Thread
{

Socket individualconnection;
int socketid;
ObjectOutputStream out;
ObjectInputStream in;
String message;


public Connecthandler(Socket s, int i)
{
individualconnection = s;
socketid = i;
}

void sendMessage(String msg)
{
try{
out.writeObject(msg);
out.flush();
System.out.println("client>" + msg);
}
catch(IOException ioException){
ioException.printStackTrace();
}
}

public void run()
{

int clubId;
String clubName;
String clubEmail;
int fundsAvailTransfer;
String agentName;
int agentId;
String agentEmail;
String username;
int password;
ClubUser loggedInUser = null;
try
{

out = new ObjectOutputStream(individualconnection.getOutputStream());
out.flush();
in = new ObjectInputStream(individualconnection.getInputStream());
System.out.println("Connection"+ socketid+" from IP address "+individualconnection.getInetAddress());


//Commence
do
{
ArrayList<ClubUser> clubuser = new ArrayList<ClubUser>();
do
{
sendMessage("Please Enter 1 to Register or 2 to Login");
message = (String)in.readObject();
}while(!message.equals("1")&&!message.equals("2"));

if(message.equals("1"))
{

sendMessage("Enter 1 to Register Club or 2 to Register Player Agent");
message = (String)in.readObject();
// Club Registration

if(message.equals("1"))
{
sendMessage("Enter Club Name: ");
message = (String)in.readObject();
clubName = message;

sendMessage("Enter Club Id: ");
message = (String)in.readObject();
clubId = Integer.parseInt(message);

sendMessage("Enter Club Email: ");
message = (String)in.readObject();
clubEmail = message;

sendMessage("Enter Funds Available To Transfer: ");
message = (String)in.readObject();
fundsAvailTransfer = Integer.parseInt(message);

ClubUser cu = new ClubUser(clubId, clubName, clubEmail, fundsAvailTransfer);
clubuser.add(cu);

for (ClubUser u : clubuser)
{
System.out.println(u);
}// for

}// innerif


}// outerif

else if(message.equals("2"))
{
sendMessage("Please enter Club Name: ");
message = (String)in.readObject();
username = message;

sendMessage("Please enter Club Id: ");
message = (String)in.readObject();
password = Integer.parseInt(message);

for (ClubUser user : clubuser)
{
if (user.getClubId()==(password))
{
if (user.getClubName().equalsIgnoreCase(username))
{

loggedInUser = user;

// when a user is found, "break" stops iterating through the list
// break;
}
}
}// for
if(loggedInUser != null)
{
sendMessage("welcome!!!!!! " + loggedInUser.getClubName());
}
// problem here
else
{
sendMessage("try again ");
}


}// else if


sendMessage("Y to repeat or N to terminate");

message = (String)in.readObject();
}while(message.equalsIgnoreCase("Y"));

}
catch (IOException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

finally
{
try
{
out.close();
in.close();
individualconnection.close();
}


catch (IOException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
}


}

}

我的客户端代码

客户端代码似乎没问题。

import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.*;
import java.util.Scanner;
//test
public class Client
{

private Socket connection;
private String message;
private Scanner console;
private String ipaddress;
private int portaddress;
private ObjectOutputStream out;
private ObjectInputStream in;

public Client()
{
console = new Scanner(System.in);

System.out.println("Enter the IP Address of the server");
ipaddress = console.nextLine();

System.out.println("Enter the TCP Port");
portaddress = console.nextInt();

}

void sendMessage(String msg)
{
try{
out.writeObject(msg);
out.flush();
System.out.println("client>" + msg);
}
catch(IOException ioException){
ioException.printStackTrace();
}
}


public static void main(String[] args)
{
Client temp = new Client();
temp.clientapp();
}

public void clientapp()
{

try
{
connection = new Socket(ipaddress,portaddress);

out = new ObjectOutputStream(connection.getOutputStream());
out.flush();
in = new ObjectInputStream(connection.getInputStream());
System.out.println("Client Side ready to communicate");


/// Client App.

do
{
do
{
message = (String)in.readObject();
System.out.println(message);
message = console.next();
sendMessage(message);
}while(!message.equalsIgnoreCase("1")&&!message.equalsIgnoreCase("2"));

if(message.equals("1"))
{
// reg as club or agent
message = (String)in.readObject();
System.out.println(message);
message = console.next();
sendMessage(message);

if(message.equals("1"))
{
// club name
message = (String)in.readObject();
System.out.println(message);
message = console.next();
sendMessage(message);
// club id
message = (String)in.readObject();
System.out.println(message);
message = console.next();
sendMessage(message);
// club email
message = (String)in.readObject();
System.out.println(message);
message = console.next();
sendMessage(message);
// club funds
message = (String)in.readObject();
System.out.println(message);
message = console.next();
sendMessage(message);
}// inner if

}// outer if

else if(message.equals("2"))
{
// club name
message = (String)in.readObject();
System.out.println(message);
message = console.next();
sendMessage(message);
// club id
message = (String)in.readObject();
System.out.println(message);
message = console.next();
sendMessage(message);

// welcome message
message = (String)in.readObject();
System.out.println(message);


}// outer else if

message = (String)in.readObject();
System.out.println(message);
message = console.next();
sendMessage(message);

}while(message.equalsIgnoreCase("Y"));

out.close();
in.close();
connection.close();
}

catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

}

}


my clubuser class with getters and setters



public class ClubUser extends server{
int clubId;
String clubName;
String clubEmail;
int fundsAvailTransfer;

public ClubUser(int clubId, String clubName, String clubEmail, int fundsAvailTransfer) {
super();
this.clubId = clubId;
this.clubName = clubName;
this.clubEmail = clubEmail;
this.fundsAvailTransfer = fundsAvailTransfer;
}

int getClubId() {
return clubId;
}

private void setClubId(int clubId) {
this.clubId = clubId;
}

String getClubName() {
return clubName;
}

private void setClubName(String clubName) {
this.clubName = clubName;
}

private String getClubEmail() {
return clubEmail;
}

private void setClubEmail(String clubEmail) {
this.clubEmail = clubEmail;
}

private int getFundsAvailTransfer() {
return fundsAvailTransfer;
}

private void setFundsAvailTransfer(int fundsAvailTransfer) {
this.fundsAvailTransfer = fundsAvailTransfer;
}

@Override
public String toString() {
return "ClubUser clubId=" + clubId + ", clubName=" + clubName + ", clubEmail=" + clubEmail
+ ", fundsAvailTransfer=" + fundsAvailTransfer;
}


}

我想要的另一件事是将添加的用户详细信息保存在文件中,然后在程序开始时用它们初始化数组。我该怎么做?

最佳答案

在 Connecthandler::run() 方法中,尝试将 ArrayList“clubUser”移到最外层 do/while 的范围之外。这样它就不会在每次迭代时重新初始化。

// Place the list initialization here; outside the scope of the outer-most do loop
ArrayList<ClubUser> clubuser = new ArrayList<ClubUser>();
do {
// remove from here.
// ArrayList<ClubUser> clubuser = new ArrayList<ClubUser>();
do {
sendMessage("Please Enter 1 to Register or 2 to Login");
message = (String) in.readObject();
} while (!message.equals("1") && !message.equals("2"));

...
}

关于java - 为什么我的服务器跳过 if 语句并转到 else 部分(重试)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59675797/

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