我需要一些代码帮助。 我需要;
程序应检查数组中的“?”并说出不允许的问题
并检查他们输入的主题,并要求他们“讲述更多有关 'x'“
结束,如果在Joption中按下了“取消”或它包含“再见”
到目前为止,这是我的代码;
import java.util.*;
import javax.swing.*;
public class ATSEStage2 {
public static void main(String[] args) {
String tmma = "Tell me more about ";
JOptionPane.showMessageDialog(null, "Hi, Welcome to ATSE \nStarting Input taking Phase...");
int N = Integer.parseInt(JOptionPane.showInputDialog("How many Topics will you be talking about?"));
double [] topics;
topics = new double [N];
String [] kw = new String[N];
String [] chat = new String [N];
int i=0; int a=1;
while(i<N) {
kw [i] = JOptionPane.showInputDialog("Enter Topic "+a);
System.out.println(kw [i]);
i++;
a++;
}
i=0;a=0;
String formattedString = Arrays.toString(kw)
.replace("[", "")
.replace("]", "");
JOptionPane.showMessageDialog(null,
"The Topics you entered were, " +formattedString + "\nStarting Chatting phase...");
int e=0;
List<String> list = Arrays.asList(kw);
chat [e] = JOptionPane.showInputDialog(null, tmma + formattedString);
List<String> list1 = Arrays.asList(chat);
while(e==0) {
If (list1.contains("?"));{chat [e] = JOptionPane.showInputDialog(null, tmma + formattedString);
e++;
}
}
}
谢谢!。
答案 0 :(得分:0)
@Lonath Senevirathne,您正在使用contains()
类的List
方法。如果将其打开,则会看到描述为Returns true if this list contains the specified element. More formally, returns true if and only if this list contains at least one element e such that (o==null ? e==null : o.equals(e)).
。如果您希望能够检查/验证数组中的单词并在包含特定单词的情况下对其进行循环,则应遍历所有列表元素
while (e == 0) {
for (String st : list1) {
if (st != null && st.contains("?")) {
chat[e] = JOptionPane.showInputDialog(null, tmma + formattedString);
e++;
}
}
}
使用while loop
int idx = 0;
while(idx < list1.size()) {
if (list1.get(idx) != null && list1.get(idx).contains("?")) {
chat[e] = JOptionPane.showInputDialog(null, tmma + formattedString);
e++;
}
idx++;
}
如果while loop
不包含list1
,则您还有无限的?