Java Socket永无止境循环

时间:2014-09-30 15:35:15

标签: java sockets loops process

您好我正在使用Java执行命令系统。例如:您输入" date"然后你会看到时间和日期但是这个问题出现了,当我试图修复它时,它是一个永无止境的循环。这里的代码有一点解释:

switch(string){
case "date":
    //Give date

case "random":
    //The thing is when you type "random" it will wait on the next input from the user and store it in this variable.

    while(!string2.equals("BREAK")){
        String string2 = (String) inputdata.readObject();
        //It will process the users next information if the user NOT type "BREAK".
    }
    break;

    //The problem is that the variable "string2" must be outside the while loop BUT it NEEDS in the loop. I tried with do while loop but the same problem happend.
}

如果我必须解决这个问题,我必须在循环中的循环循环中循环.... 谢谢你的回答。 :D评论,如果你不明白,我在开头就不明白我的问题。

1 个答案:

答案 0 :(得分:1)

你可以在循环之外声明string2然后在里面引用它吗?

String string2;
switch(string){
case "date":
//Give date

case "random":
//The thing is when you type "random" it will wait on the next input from the user and store it     in this variable.

while(!string2.equals("BREAK")){
    string2 = (String) inputdata.readObject();
    //It will process the users next information if the user NOT type "BREAK".
}
break;

//The problem is that the variable "string2" must be outside the while loop BUT it NEEDS in the loop. I tried with do while loop but the same problem happend.
}