Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/loops/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java For或While循环跳过输入_Java_Loops_Fencepost - Fatal编程技术网

Java For或While循环跳过输入

Java For或While循环跳过输入,java,loops,fencepost,Java,Loops,Fencepost,可能重复: 我正在创建一个客户端程序,它需要从服务器读取字符串和整数。根据接收到的整数,它会向GUI添加一些标签。到目前为止,我的程序读取整数,但跳过字符串。以下输出是我尝试将整数写入程序时程序的输出: 服务器写入:1 服务器写入:1 系统打印:1 系统打印:j1 系统打印:名称 问题是我无法写入字符串,因为它跳过了字符串。如何避免此问题(请注意,我还尝试了for循环) 我的代码如下: int times = client.reciveCommando(); int o = 0; Syst

可能重复:

我正在创建一个客户端程序,它需要从服务器读取字符串和整数。根据接收到的整数,它会向GUI添加一些标签。到目前为止,我的程序读取整数,但跳过字符串。以下输出是我尝试将整数写入程序时程序的输出:

  • 服务器写入:1
  • 服务器写入:1
  • 系统打印:1
  • 系统打印:j1
  • 系统打印:名称
问题是我无法写入字符串,因为它跳过了字符串。如何避免此问题(请注意,我还尝试了
for
循环)

我的代码如下:

int times = client.reciveCommando();
int o = 0;
System.out.println(times);

while (o != times) {
  int j = client.reciveCommando();
  System.out.println("j"+ j);
  String name = client.reciveString();
  System.out.println("Name " +name);
  createUser(j, name);
  o++;

}
createUser方法:

private void createUser(int j, String reciveChat) {
  if (j == 1) {
    chatPerson1.setVisible(true);
    lbl_Chatperson1_userName.setVisible(true);
    lbl_Chatperson1_userName.setText(reciveChat);
  } else if (j == 2) {
    lbl_chatPerson2.setVisible(true);
    lbl_userName2.setVisible(true);
    lbl_userName2.setText(reciveChat);
  } else {
    chatPerson3.setVisible(true);
    lbl_userName3.setVisible(true);
    lbl_userName3.setText(reciveChat);
  }
}
client.reciveCommando方法:

public int reciveCommando() throws IOException{
  Integer i = input.nextInt();
  return i;
}
client.recinvesting方法:

public String reciveString(){
  String x = input.nextLine();
  return x;
}
希望有人能帮我:)


提前谢谢。

在循环代码中,我看不到任何地方您正在增加
o
或更改
次的值。因此,要么循环被完全跳过(即:times=0),要么代码中的某个其他位置正在修改循环变量(
o
)或循环条件(
times
)——这两种情况下都是非常糟糕的编码

在读取循环时,您的循环变量/增量规则应该非常清楚,并且不需要读取可能在循环迭代期间修改值的其他方法/etc,就可以很容易地识别开始/停止条件


我的直接猜测是
times=0
,否则您将处于一个无休止的循环中

我找到了我问题的解决方案,结果很简单

首先让我解释一下我的想法

当我的程序运行while循环时,它基本上跳过了应该从服务器接收输入的行。我发现它这样做的原因是input.nextLine();为空,这在您读取输入的api时很有意义。nextLine()

使此扫描仪前进到当前行,并返回跳过的输入。此方法返回当前行的其余部分,不包括末尾的任何行分隔符。该位置设置为下一行的开头。 由于此方法继续在输入中搜索行分隔符,因此如果不存在行分隔符,它可能会缓冲所有搜索该行的输入。 返回: 跳过的那行

由于我试图获取的行是一个空行,它将跳过并将name设置为“”

以下是我的程序的完整代码,它目前正在运行:

while循环:

            while (o != times) {
                int j = client.reciveCommando();
                System.out.println("j"+ j);
                    String name = client.reciveString();
                    System.out.println("Name " +name);
                    createUser(j, name);    
                o++;
            }
客户端。重新投资()

    public String reciveString(){

    String x = input.next();
    return x;
}
    private void createUser(int j, String reciveChat) {
    if (j == 1) {
        chatPerson1.setVisible(true);
        lbl_Chatperson1_userName.setVisible(true);
        lbl_Chatperson1_userName.setText(reciveChat);

    }else if (j == 2) {
        lbl_chatPerson2.setVisible(true);
        lbl_userName2.setVisible(true);
        lbl_userName2.setText(reciveChat);
    }else if (j == 3){
        chatPerson3.setVisible(true);
        lbl_userName3.setVisible(true);
        lbl_userName3.setText(reciveChat);}
createUser()

    public String reciveString(){

    String x = input.next();
    return x;
}
    private void createUser(int j, String reciveChat) {
    if (j == 1) {
        chatPerson1.setVisible(true);
        lbl_Chatperson1_userName.setVisible(true);
        lbl_Chatperson1_userName.setText(reciveChat);

    }else if (j == 2) {
        lbl_chatPerson2.setVisible(true);
        lbl_userName2.setVisible(true);
        lbl_userName2.setText(reciveChat);
    }else if (j == 3){
        chatPerson3.setVisible(true);
        lbl_userName3.setVisible(true);
        lbl_userName3.setText(reciveChat);}

谢谢你的回复,我一定会投票支持你:)

你在哪里?(可能是我瞎了?)如果times==0,while循环将跳过一个字符串。这可能是您正在寻找的内容,也可能对您有所帮助。好的,我的复制粘贴在while循环结束时失败了,它执行o++;好的,基本上是这样的:首先程序启动,然后它询问次数我将时间设置为1(例如),然后它再次询问我另一个数字(这是客户端的id),我将其设置为1。然后系统打印1,j1,名称。如果我试图设置一个字符串,我会得到一个错误,因为发布的东西不是整数(它指的是reciveCommando),它是一个整数,我不懂。“因为发布的东西不是int(它指的是reciveCommando)”。发布了什么内容?@int j=client.reciveCommando();程序要求输入一个整数,然后程序跳转到“System.out.Println(“name”+name);”并重复循环(将o设置为+1),而不是要求我输入字符串!所以它基本上跳过了这一行:String name=client.reciveString();什么是
输入
?你试过使用调试器吗?当您单步执行每一行代码时会发生什么?