简单switch case中的代码或if语句不执行

时间:2016-03-22 22:58:45

标签: c# console

在下面,我的代码在开关盒“c”内部不执行。什么都没发生!如果输入为“q”,则isOwner = false执行得很好。这对我来说毫无意义。请帮忙!

// Customer information
string customerName = "";
string customerPassword = "";
int customerCredits = 0;

string input = "";

Console.WriteLine ("Welcome to my console Pet Store");
Console.WriteLine ("Type in your name?");
customerName = Console.ReadLine ();
Console.WriteLine ("Type in your password?");
customerPassword = Console.ReadLine ();

if (customerName == "owner" && customerPassword == "1234") {

    bool isOwner = true;    // Launch the owner interface when isOwner is true

    while (isOwner) {
        Console.Clear ();
        Console.WriteLine ("You are logged in as Owner");
        Console.WriteLine ("[q] Quit");
        Console.WriteLine ("[c] Create new product");
        Console.WriteLine ("[d] Create new animal");
        Console.WriteLine ("[i] View all items in the store");
        Console.WriteLine ("[t] View all customers");

        // string input;
        input = Console.ReadLine ();

        switch (input) {
            case "q":
                isOwner = false;
                break;
            case "c":
                Console.Clear ();
                Console.WriteLine ("What type of product do you want to create?");
                break;
        }
    }
} // END of owner interface

顺便说一句:我也尝试过if语句,但结果相同。

2 个答案:

答案 0 :(得分:0)

看起来很奇怪。但我有一个假设: 有可能是char' c'不是英国人?许多语言都有相似的字母,但这些字母的代码不同。

尝试删除' c'然后使用eng键盘布局再次键入它。

答案 1 :(得分:0)

代码正在运行,它只是在显示提示后没有等待。打印提示后,请尝试添加Console.ReadKey();之类的内容:Console.WriteLine("What type of product do you want to create?")

switch (input) {
        case "q":
            isOwner = false;
            break;
        case "c":
            Console.Clear ();
            Console.WriteLine("What type of product do you want to create?");
            Console.ReadKey();
            break;
    }