如果在Java中的Else语句

时间:2013-12-14 11:11:51

标签: java

该程序应该是用户输入他选择的类别,但当我尝试运行它时,我输入“非洲”,else语句上的命令出现。

package finalProject;

import java.util.Scanner;

public class Lol {
    static String category;

    static Scanner x = new Scanner(System.in);
    public static void main(String[] args) {

        System.out.println("Welcome to Shang-ri La Plaza Mall Restaurant Inquiry! \n");
        System.out.println("Choose from below which cuisine do you prefer. \n" +
            "African or American");
        category = x.next();

        if (category == "African") {
             System.out.println("Options are found below with their corresponding price range. \n" +
                 "Wendy's Hamburger - P199 & Below");
        } else {
             System.out.println("Options are found below with their corresponding price range. \n" +
                 "Clawdaddy's Great American Picnic - P500 - P999 \n" +
                 "California Pizza Kitchen - P200 - P499 \n" + 
                 "Tender Bob's - P200 - P499 \n" +
                 "Steak Escape - P200 - P499 \n" +
                 "Balboa - P200 - P499 \n" +
                 "Billy Rock Bar & Restaurant - not found \n" +
                 "Clawdaddy Osteria Americana - P500 - P999 \n" +
                 "Gourmet To Go - P200 - P499 \n" +
                 "Maple - P500 - P999 \n" +
                 "Quiznos - P200 - P499 \n" +
                 "Texas Roadhouse Grill - P500 - P999");
        }
    }
}

2 个答案:

答案 0 :(得分:3)

尝试使用

if (category.equals("African") { 

在比较字符串时,从不(曾经)使用==。这将测试对象的相同性而不是内容(参见How do I compare strings in Java?)。

答案 1 :(得分:0)

使用

category.equals("African");

您正在比较字符串值