在循环java中搜索字符串中的值

时间:2015-03-23 11:41:39

标签: java

我设置的while循环有什么问题?

    String animal;
    animal = user_input.next();
    animal = animal.toLowerCase();

    while ((animal != "c") && (animal != "d")) {
        System.out.println("Invalid input, please enter c for a cat or d for a dog");
        animal = user_input.next();
        animal = animal.toLowerCase();
    }
    if (animal == "c") {
        System.out.println("you have a cat");
    } else {
        System.out.println("you have a dog");
    }

0 个答案:

没有答案