gpt4 book ai didi

Java 服务器猜谜游戏

转载 作者:塔克拉玛干 更新时间:2023-11-03 05:26:14 26 4
gpt4 key购买 nike

我正在创建一个服务器,客户可以在其中连接以玩猜谜游戏,他们也可以从中获得积分。

目前我唯一的问题是,每当我的客户猜对数字时,它就会跳转到服务器并说“服务器为空”。我希望猜谜游戏一直持续到客户输入“再见” - 给出了他/她的分数。

这是我的代码,您能否指出我哪里出错了,并建议我如何实现我想要的。我认为问题出在协议(protocol)中,我可能只需要将 while 放在正确的位置,所以这是第一个。谢谢大家!补充一下,变量的命名很奇怪我知道它以前是一个敲敲笑话服务器

协议(protocol)

import java.util.*;

public class KKProtocol {
int guess = 0, number = new Random().nextInt(100) + 1;
int score = 0;
Scanner scan = new Scanner(System.in);


public String processInput(String theInput) {
String theOutput = null;

System.out.println("Please guess the number between 1 and 100.");

while (guess != number) {
try {
if ((guess = Integer.parseInt(scan.nextLine())) != number) {
System.out.println(guess < number ? "Higher..." : "Lower...");
}
else {
System.out.println("Correct!");
score = 1;
}
}
catch (NumberFormatException e) {
System.out.println("Please enter a valid number! If you want to Quit just say'Goodbye'");
}

}



return theOutput;
}}

服务器

import java.net.*;
import java.io.*;

public class KKServer {
public static void main(String[] args) throws IOException {
ServerSocket serverSocket = null;
boolean listening = true;
try {
serverSocket = new ServerSocket(4040);
} catch (IOException e) {
System.err.println("Could not listen on port: 4040.");
System.exit(-1);
}
System.err.println("Started KK server listening on port 4040");
while (listening)
new KKThread(serverSocket.accept()).start();
System.out.println("Accepted connection from client");
serverSocket.close();
}
}

线程

import java.net.*;
import java.io.*;
import java.util.Scanner;

public class KKThread extends Thread {
private Socket mySocket = null;

public KKThread(Socket inSocket) { //super("KKThread");
mySocket = inSocket;
}
public void run() {

try {
PrintWriter out = new PrintWriter(mySocket.getOutputStream(), true);
Scanner in = new Scanner(mySocket.getInputStream());

String inputLine, outputLine;
KKProtocol kkp = new KKProtocol();

outputLine = kkp.processInput(null); // first time only
out.println(outputLine); // (Should be "Knock Knock")

while (true) {
inputLine = in.nextLine(); // read in client input
outputLine = kkp.processInput(inputLine); // get reply from protocol
out.println(outputLine); // send it out to socket
if (outputLine.equals("Bye"))
break;
}
out.close();
in.close();
mySocket.close();

} catch (Exception e) {
System.err.println("Connection reset"); //e.printStackTrace();
}
}
}

客户端

import java.io.*;
import java.net.*;
import java.util.Scanner;

public class KKClient {
public static void main(String[] args) throws IOException {

Socket kkSocket = null;
PrintWriter out = null;
Scanner in = null;

try {
kkSocket = new Socket("127.0.0.1", 4040);
out = new PrintWriter(kkSocket.getOutputStream(), true);
in = new Scanner(new InputStreamReader(kkSocket.getInputStream()));
} catch (UnknownHostException e) {
System.err.println("Don't know about host.");
System.exit(1);
} catch (IOException e) {
System.err.println("Couldn't get I/O for the connection");
System.exit(1);
}
Scanner stdIn = new Scanner(System.in);
String fromServer = in.nextLine();
while (true) {
System.out.println("Server: " + fromServer);
if (fromServer.equals("Bye."))
break;
out.println(stdIn.nextLine());
fromServer = in.nextLine();
}
out.close();
in.close();
stdIn.close();
kkSocket.close();
}
}
'

为什么它会跳转到服务器并显示“server null”?我如何继续猜测直到客户输入“再见”?

最佳答案

您当前未在 KKProtocol.processInput() 中为 score 分配服务器响应,因此返回 null 而不是导致您看到的消息:

Server: null

你可以使用:

theOutput = Integer.toString(score);

此外,您的分数 固定为1,因此您可能希望设计一个可能基于所用猜测次数的评分系统。

关于Java 服务器猜谜游戏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13903051/

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