【问题标题】:Java Socket never ending loopJava Socket 永无止境的循环
【发布时间】:2014-09-30 15:35:14
【问题描述】:

你好,我正在用 Java 做一个命令系统。举个例子:你输入“日期”,你会看到时间和日期,但是这个问题出现了,当我试图修复它时,这是一个永无止境的循环。这是我的代码和一些解释:

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 如果您不明白,请发表评论,我一开始也没有理解我的问题。

【问题讨论】:

  • 您是否尝试过 equalsIgnoreCase 比较...有时由于区分大小写而发生
  • 你为什么用readObject()来读取用户输入的信息?
  • 为什么不用readObject(),我应该用什么?
  • inputdata是什么类型?
  • 是我为输入流写的变量

标签: java sockets loops process


【解决方案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.
}

【讨论】:

    猜你喜欢
    • 2012-12-04
    • 2016-06-04
    • 2023-03-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-11-14
    • 2020-03-14
    • 1970-01-01
    相关资源
    最近更新 更多