简体   繁体   English

Java服务器猜测游戏

[英]Java Server Guessing Game

I'm creating a server where clients connect to play a guessing game they are also to get points from doing this. 我正在创建一个服务器,客户端连接它来玩猜谜游戏,他们也是为了获得积分。

My only problem at the moment is that whenever my client guesses the number correctly it jumps to the server and says 'server null'. 我目前唯一的问题是每当我的客户端正确猜出数字时,它会跳转到服务器并说'server null'。 I want the guessing game to continue until the client inputs 'goodbye' - on which his/her score is given. 我希望猜测游戏能够继续,直到客户输入'再见' - 他/她得分。

Here is my code could you point out where I've gone wrong and advise me on how I would achieve what I want. 这是我的代码,你可以指出我出错的地方,并告诉我如何实现我想要的。 I think the problem is in protocol I probably just need to put the while in the correct place, so that's up first. 我认为问题出在协议中我可能只需要将时间放在正确的位置,这样就可以了。 Thanks folks! 谢谢大家! Just to add, the variables are named oddly I'm aware of this it was previously a knock knock joke server 只是添加,变量名称奇怪我知道它以前是一个敲敲笑话服务器

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;   
    }}

Server 服务器

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();
        }
}

Thread 线

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();
        }
    }
}

Client 客户

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();
    }
}
'

Why does it jump to the server and says 'server null'? 为什么它跳转到服务器并说'server null'? How do I continue the guessing until the client inputs 'goodbye'? 如何在客户输入“再见”之前继续猜测?

You are currently not assigning the server response for the score in KKProtocol.processInput() so a null is returned instead resulting in the message that you see: 您当前没有为KKProtocol.processInput()score分配服务器响应,因此返回null而不是导致您看到的消息:

Server: null

You could use: 你可以使用:

theOutput = Integer.toString(score);

Also your score is fixed at 1 so you may wish to devise a scoring system perhaps based on the number of guesses used. 此外,您的score固定为1因此您可能希望根据所使用的猜测数量来设计评分系统。

In you processInput() method, you are not returning any value, but allways null . processInput()方法中,您不返回任何值,但始终为null It looks like that null value is transformed into the String "null" on output and sent to the client. 看起来这个null值在输出中转换为字符串“null”并发送到客户端。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM