java - 我的 Java 网络项目中存在循环和流控制问题

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

}

最佳答案

您需要在循环外生成随机数,否则每次猜测都会改变。写入字符串后,您还必须在服务器中使用 out.flush() - 仅在获胜情况下才这样做。 while(timer == 7)可以完全删除,此时执行时您可以无条件写入客户端。

关于java - 我的 Java 网络项目中存在循环和流控制问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27973900/

相关文章:

JAVA:将值从一个类传递到另一个类

java - 如何从 GWT 调用 RESTFUL 服务?

java - 这些操作中哪一个在节省内存和 CPU 方面更好? ( java )

linux - 在bash中如何循环遍历以 "abc"开头的目录和目录 "special-dir"

c - 循环时求平均值,总和不会清除,因此会不断添加上一个循环条目中的数字

r - 累积和直到达到最大值,然后在下一行从零开始重复

java - 当在 eclipse android 真实设备上运行我的程序时,我在 LogCat 中收到许多错误,所有这些错误是什么以及如何修复它们?

java - 将 SqlCipher 与 Android 应用程序集成(在 Windows 上)

java - IPageLayout下添加keyevent切换ViewPart

javascript - 循环遍历所有不是文件类型的输入项