0
import java.util.Scanner;
import static java.lang.System.out;
import static java.lang.System.in;
public class AI_Core {

     /**
 * @param args
 */
public static void main(String[] args) {
    String Input;
    String HOY_Input;
    Scanner Command = new Scanner(in);
    Scanner HOY_Response = new Scanner(in);
    out.println("Hello Sir, how can I help you?");
    while(true){
        Input = Command.nextLine();
        if((Input.equals("Hello")) || (Input.equals("Hey M"))) {
            out.println("Hello Sir, how are you?");
            HOY_Input = HOY_Response.nextLine();
            if((HOY_Input.equals("Great!")) || (HOY_Input.equals("Alright"))) {
                out.println("Glad to hear it sir.");
            }
            if((HOY_Input.equals("Not so good")) || (HOY_Input.equals("Been better"))) {
                out.println("I'm sorry to hear that sir, just let me know if I can help in any way.");
            }
        }
        if(Input.equals("exit")) {
            out.println("Goodbye Sir.");
            //out.wait();
            System.exit(0);
        }
        else {
            out.println("I'm sorry sir, but I don't quite understand the term: ");
            out.print(Input);
            out.print(", could you please check your spelling and try again?");
        }
    }
}

}

Else 语句在 If 语句之后执行,如控制台所示:

先生您好,请问有什么可以帮助您的吗?
嘿 M
你好先生,你好吗?
伟大的!
很高兴听到先生。
对不起,先生,但我不太明白这个词:嘿,M,你能检查一下你的拼写,然后再试一次吗?

4

2 回答 2

3

当你说if(Input.equals("exit")),你已经开始了另一个if。与“exit ”Input不匹配,因此执行 else。

看起来您只希望执行 3 个总案例中的一个。在这种情况下,您希望else if将其连接到第一个if

else if(Input.equals("exit")) {

这样,只有在前两个语句都 yieldI'm sorry sir, but I don't quite understand the term:时才会打印。iffalse

于 2013-05-03T16:45:57.557 回答
0

问题是您的 else 语句仅引用它之前的 if 语句。第一个 if 语句是完全独立的。只要输入不是“退出”,else 部分就会执行。

您需要else if连接所有三个语句,以便只执行一个:

else if(Input.equals("exit")) {

此页面可能会有所帮助:

http://docs.oracle.com/javase/tutorial/java/nutsandbolts/if.html

于 2013-05-03T16:49:49.027 回答