我的Java Networking项目中的循环和流控制问题

时间:2015-01-15 22:08:06

标签: java loops

我正在尝试编写一个数字猜谜游戏,其中有一个客户端接受猜测并输出结果,一个接收所述猜测的服务器,将它们与随机生成的数字进行比较,并告诉客户端猜测是否过高,低或正确。目前我只是不明白为什么服务器没有回复客户端。我想我在某个地方犯了一个流量控制错误,但我对我的错误在哪里感到困惑..非常感谢任何帮助!

客户端:

public class Client {

    public static void main(String[] args) throws IOException {
        System.out.println("This is Number Guessing Game. \nChoose any number between 1 to 1000 : ");
        Scanner s = new Scanner(System.in);



        try {  
            Socket client = new Socket("localhost", 8093);



            DataOutputStream out = new DataOutputStream(
                    new BufferedOutputStream(client.getOutputStream()));
            DataInputStream in = new DataInputStream(new BufferedInputStream(
                    client.getInputStream()));

            boolean haswon=false;
            int i=0;

            while(i<11){
                i++;
                int guess = s.nextInt();
                out.writeInt(guess);  
                out.flush();

                String result = in.readUTF();  
                System.out.println(result);
                haswon = in.readBoolean();
                if(haswon=true){
                    System.out.println("You Win! :D");
                    i=11;}else{continue;}


            }

            client.close();
            System.out.println("Game over. Thank you");
            System.exit(0);

        }catch (IOException ioe) {
            System.err.println(ioe);
        }
    }
}

服务器:

public class Server {

    public static void main(String[] args) throws IOException {
        ServerSocket socket = null;
        Socket client = null;
        boolean haswon;

        int guess = 0;
        int random = 0;
        int timer = 0;
        String end;
        String lwrong;
        String hwrong;
        String win;


                try {
                    socket = new ServerSocket(8093);
                    System.out.println("The server socket has started and ready to receive client connection.");
                    client = socket.accept();


                    DataInputStream input = new DataInputStream(new BufferedInputStream(
                            client.getInputStream()));
                    DataOutputStream output = new DataOutputStream(new BufferedOutputStream(
                            client.getOutputStream()));


                    while(timer<11){ 
                        guess = input.readInt(); 
                        random=(int)(Math.random()*100);
                        if (guess < random){ 
                            lwrong="Your guess was too low, try again.";
                            output.writeUTF(lwrong);
                            System.out.println("successh");
                            haswon=false;
                            output.writeBoolean(haswon);
                            timer++;                                                    
                        }else if(guess > random){
                            hwrong="Your guess was too high, try again";  
                            output.writeUTF(hwrong);
                            System.out.println("successl");
                            haswon=false;
                            output.writeBoolean(haswon);
                            timer++;    
                        }else if(guess==random){
                            win="Correct!";
                            output.writeUTF(win);
                            System.out.println("successw");
                            timer=8;
                            haswon=true;
                            output.writeBoolean(haswon);
                            output.flush();
                            timer++;
                            break;  
                        }
                        continue;


                        }
                        while(timer==7){
                            end="Out of Guesses, the correct answer was: ";
                            output.writeUTF(end);
                            output.flush();
                            break;                    
                        }








                    } catch (IOException ioe) {
                        System.err.println("Error Connecting "+ ioe);
                        return;
                    }




            client.close();
            socket.close();
                }

}

1 个答案:

答案 0 :(得分:1)

您需要在循环外生成随机数,否则每次猜测都会改变。在编写字符串后,您还必须在服务器中输出out.flush() - 您只能为win情况执行此操作。 while(timer == 7)可以完全删除,此时执行中您可以无条件地写入客户端。