[英]Condition within condition java
我是这个网站和Java的新手。
谁能帮助我找出为什么即使没有红点程序的某些部分仍无法正常工作的原因?
我在行上使用//注释。
import javax.swing.JOptionPane;
public class ChatterBot {
public static void main(String[] args) {
// TODO Auto-generated method stub
String firstName, work, sex = null;
int age;
firstName = JOptionPane.showInputDialog("Hello, my name is Chatterbox. What is your name?");
if (firstName.toLowerCase().contains("name".toLowerCase())) {
JOptionPane.showMessageDialog(null, "Welcome " + firstName.substring(firstName.lastIndexOf(" ") + 1) + "!");
sex = JOptionPane.showInputDialog(null, "Is " + firstName.substring(firstName.lastIndexOf(" ") + 1)
+ " a guy name or a woman name? (type stop to end conversation)");
} else {
JOptionPane.showMessageDialog(null, "Welcome " + firstName + "!");
sex = JOptionPane.showInputDialog(null,
"Is " + firstName + " a guy name or a woman name (type stop to end conversation)");
}
while (true)
if (sex.toLowerCase().contains("guy".toLowerCase())) {
JOptionPane.showMessageDialog(null, "Welcome friend");
work = JOptionPane
.showInputDialog("Would you like to talk about work or do you want to hear a cool story?");
if (work.toLowerCase().contains("work".toLowerCase())) {
JOptionPane.showMessageDialog(null, "Interesting");
break;
} else if (work.toLowerCase().contains("story".toLowerCase())) {
JOptionPane.showMessageDialog(null, "hola");
break;
} else if (sex.toLowerCase().contains("woman".toLowerCase())) {
//when I type woman nothing happens but the else if below for "stop" works.
age = Integer.parseInt(JOptionPane.showInputDialog(null, "How old are you?"));
if (age >= 18 && age <= 40) {
JOptionPane.showMessageDialog(null, "Dayummm");
} else if (age > 40) {
JOptionPane.showMessageDialog(null, "I don't like no cougar!");
} else {
JOptionPane.showMessageDialog(null, "I ain't no pedo. Bye!");
}
}
break;
} else if (sex.toLowerCase().contains("stop".toLowerCase())) {
JOptionPane.showMessageDialog(null, "Have a nice day.");
break;
} else {
JOptionPane.showMessageDialog(null, "Goodbye");
break;
}
}
您的if (sex.toLowerCase().contains("woman".toLowerCase()))
块嵌套在if (sex.toLowerCase().contains("guy".toLowerCase()))
块内,但它们应该是在同一级别:
if (sex.toLowerCase().contains("guy".toLowerCase())) {
JOptionPane.showMessageDialog(null, "Welcome friend");
work = JOptionPane
.showInputDialog("Would you like to talk about work or do you want to hear a cool story?");
if (work.toLowerCase().contains("work".toLowerCase())) {
JOptionPane.showMessageDialog(null, "Interesting");
break;
} else if (work.toLowerCase().contains("story".toLowerCase())) {
JOptionPane.showMessageDialog(null, "hola");
break;
}
break;
} else if (sex.toLowerCase().contains("woman".toLowerCase())) {
//when I type woman nothing happens but the else if below for "stop" works.
age = Integer.parseInt(JOptionPane.showInputDialog(null, "How old are you?"));
if (age >= 18 && age <= 40) {
JOptionPane.showMessageDialog(null, "Dayummm");
} else if (age > 40) {
JOptionPane.showMessageDialog(null, "I don't like no cougar!");
} else {
JOptionPane.showMessageDialog(null, "I ain't no pedo. Bye!");
}
} else if (sex.toLowerCase().contains("stop".toLowerCase())) {
JOptionPane.showMessageDialog(null, "Have a nice day.");
break;
} else {
JOptionPane.showMessageDialog(null, "Goodbye");
break;
}
您的if语句不正确,您缺少一个字符}
以关闭if和remove }
} else if (sex.toLowerCase().contains("woman".toLowerCase())) {
//when I type woman nothing happens but the else if below for "stop" works.
age = Integer.parseInt(JOptionPane.showInputDialog(null, "How old are you?"));
if (age >= 18 && age <= 40) {
JOptionPane.showMessageDialog(null, "Dayummm");
} else if (age > 40) {
JOptionPane.showMessageDialog(null, "I don't like no cougar!");
} else {
JOptionPane.showMessageDialog(null, "I ain't no pedo. Bye!");
}
break;
}
还建议:引入变量以防止重复代码
String sex = sex.toLowerCase()
String work = work.toLowerCase()
您也可以将小写值上的toLowerCase()删除为"story".toLowerCase()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.