Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/340.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/eclipse/9.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_Eclipse_While Loop - Fatal编程技术网

Java 如何将代码中的for循环更改为while循环?

Java 如何将代码中的for循环更改为while循环?,java,eclipse,while-loop,Java,Eclipse,While Loop,我正在建立一个聊天机器人程序。你能告诉我怎么换吗 代码中的for循环转换为代码中的while循环 我试过: int j = 0; while(j<numinputs()){ ... j++; } ... String[] inputs; inputs = new String[numinputs]; int i = 0; while (i < numinputs) { inputs[i] = JOptionPane.showInputDialog(null, "Pleas

我正在建立一个聊天机器人程序。你能告诉我怎么换吗 代码中的for循环转换为代码中的while循环

我试过:

int j = 0;
while(j<numinputs()){
...
j++;
}

...

String[] inputs;
inputs = new String[numinputs];
int i = 0;
while (i < numinputs) {
    inputs[i] = JOptionPane.showInputDialog(null, "Please enter key word " + (i + 1) + " ");
    if (inputs[i].contains("?") || inputs[i].isEmpty()) {
        JOptionPane.showMessageDialog(null, "Invalid Response");
    } else
        i = i + 1;
}
System.out.println(Arrays.toString(inputs));
for (int j = 0; j < numinputs; j++) {
    String search = JOptionPane.showInputDialog("Tell me more about" + " " + inputs[j]);
    System.out.println(search);
    if (search.contains("exit")) {
        System.exit(0);
    }
}

}
intj=0;

而(j我们可以尝试以下逻辑:

int j = 0;
while (j < numinputs) {
    String search = JOptionPane.showInputDialog("Tell me more about" + " " + inputs[j]);
    System.out.println(search);
    if (search.contains("exit")) {
        System.exit(0);
    }
    ++j;
}
intj=0;
while(j

上面的循环在逻辑上应该与您当前拥有的
循环的
相同。区别在于,虚拟变量循环计数器
j
是在
while
循环之外定义的,并且增量步骤作为循环内的一个单独行发生。

我们可以尝试以下逻辑:

int j = 0;
while (j < numinputs) {
    String search = JOptionPane.showInputDialog("Tell me more about" + " " + inputs[j]);
    System.out.println(search);
    if (search.contains("exit")) {
        System.exit(0);
    }
    ++j;
}
intj=0;
while(j
上面的循环在逻辑上应该与您当前拥有的
循环的
相同。不同之处在于,虚拟变量循环计数器
j
是在
while
循环之外定义的,并且增量步骤作为循环内的一个单独行发生。

放弃此操作

    int j = 0;
    while (j < numinputs) {
      String search = JOptionPane.showInputDialog("Tell me more about" + " " + inputs[j]);
      System.out.println(search);
      if (search.contains("exit")) {
      System.exit(0);
      }
      ++j;
   } 
intj=0;
while(j
For循环和while都是相同的

在for循环中,所有三个参数(initialization;decision;inc/dec)都在同一行中 但在While循环中,您必须拆分所有三个参数

但从逻辑上讲,for&while是相同的

希望对你有帮助

不断学习

休耕

    int j = 0;
    while (j < numinputs) {
      String search = JOptionPane.showInputDialog("Tell me more about" + " " + inputs[j]);
      System.out.println(search);
      if (search.contains("exit")) {
      System.exit(0);
      }
      ++j;
   } 
intj=0;
while(j
For循环和while都是相同的

在for循环中,所有三个参数(initialization;decision;inc/dec)都在同一行中 但在While循环中,您必须拆分所有三个参数

但从逻辑上讲,for&while是相同的

希望对你有帮助

不断学习


谢谢Tim,不过我已经尝试过了:System.out.println(Arrays.toString(inputs));int j=0;while(jI)得到了这个错误:线程“main”java.lang.ArrayIndexOutOfBoundsException中的异常:ChatBot1.main(ChatBot1.java:26)中长度2的索引2超出了界限。谢谢Tim,但是我尝试过:System.out.println(Arrays.toString(inputs));int j=0;while(jI)获取此错误:线程“main”java.lang.ArrayIndexOutOfBoundsException中的异常:索引2超出ChatBot1.main(ChatBot1.java:26)处长度2的界限请编辑您的问题并为
循环显示完整且正常运行的
。我已添加了循环的功能。请编辑您的问题并为
循环显示完整且正常运行的
。我已添加循环的功能谢谢@Sivareddy的帮助,但是我遇到了另一个错误:线程异常“main”java.lang.ArrayIndexOutOfBoundsException:ChatBot1.main(ChatBot1.java:25)长度2的索引2越界感谢@Sivareddy的帮助,但是我遇到了另一个错误:线程“main”java.lang.ArrayIndexOutOfBoundsException:ChatBot1.main(ChatBot1.java:25)长度2的索引2越界