为了账号安全,请及时绑定邮箱和手机立即绑定

嵌套while循环迭代

嵌套while循环迭代

阿波罗的战车 2021-04-27 21:22:09
巢式while循环发生问题,导致以下问题:线程“主”中的异常java.lang.ArrayIndexOutOfBoundsException:Chatbot.main中为2(Chatbot.java:25)我不太确定是什么引起了ArrayIndexOutOfBoundsException。这样做的部分要求是,我不允许使用while循环以外的任何其他形式的循环。也不能使用任何continue,break等任何见解将不胜感激。尼克下面的代码:import javax.swing.JOptionPane;public class Chatbot {public static void main(String[] args) {    String[] topics;    int numTopics = Integer.parseInt(JOptionPane.showInputDialog("How many topics would you like to talk about?"));    while (numTopics < 1) {        numTopics = Integer.parseInt(JOptionPane.showInputDialog("Error! Please enter a positive number of topics!"));    }    int i = 0;    topics = new String[numTopics];    while (i < topics.length) {        topics[i] = JOptionPane.showInputDialog("Please enter topic " + i);        while (topics[i].contains("?")) {            topics[i] = JOptionPane.showInputDialog("I'll ask the questions here, Please enter topic " + i);        }        i = i + 1;    }    String dialog1 = JOptionPane.showInputDialog("Tell me more about " + topics[numTopics]); }}
查看完整描述

1 回答

?
HUH函数

TA贡献1836条经验 获得超4个赞

更改topics[numTopics]topics[numTopics-1]你的最后一行的代码,因为指数在0阵列启动

String dialog1 = JOptionPane.showInputDialog("Tell me more about " + topics[numTopics-1]);



查看完整回答
反对 回复 2021-05-12
  • 1 回答
  • 0 关注
  • 147 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信