根据用户选择的内容确定输出

时间:2010-09-09 04:30:17

标签: c# loops

如果用户输入A就可以工作,那么他们输入B就可以工作了,如果他们输入E就可以工作它也可以工作但如果然后他们输入A重新启动程序就会关闭。

const double COMMRATE = 0.10;

const double COMMRATE = 0.10; 

string inputstring; 

char response; 
Console.Write("Do you want to Calculate Sales A or B or E..."); 
inputstring = Console.ReadLine(); 
response = Convert.ToChar(inputstring);

    while (response == 'A')
     {
    Console.WriteLine("Enter amount of sales");
    string salesStr = Console.ReadLine();
    Console.WriteLine(Double.Parse(salesStr) * COMMRATE);
    Console.WriteLine("Enter sales member to continue or Z to exit");
    response = Convert.ToChar(Console.ReadLine());
     }
    while (response == 'B')
    {
        Console.WriteLine("Enter amount of sales");
        string salesStr = Console.ReadLine();
        Console.WriteLine(Double.Parse(salesStr) * COMMRATE);
        Console.WriteLine("Enter sales member to continue or Z to exit");
        response = Convert.ToChar(Console.ReadLine());
    }
    while (response == 'E')
     {
         Console.WriteLine("Enter amount of sales");
    string salesStr = Console.ReadLine();
    Console.WriteLine(Double.Parse(salesStr) * COMMRATE);
    Console.WriteLine("Enter sales member to continue or Z to exit");
    response = Convert.ToChar(Console.ReadLine());
       }

3 个答案:

答案 0 :(得分:4)

因为您的代码仅按响应类型循环。 它在响应为A时迭代,然后在B时迭代,然后在E时迭代。意味着它只能在该序列中运行

也许你应该看看像:

do
{
    Console.WriteLine("EnterNextCommand");
    inputstring = Console.ReadLine();
    response = Convert.ToChar(inputstring);
    switch (response)
    {
        case 'A':
        case 'a':
        //case A logic
        break;
        case 'B':
        case 'b':
        //case B logic
        break;
        //etc.
        default:
        //they enter something you're not handling
        break;
    }

}
while (inputstring != "Z")

答案 1 :(得分:3)

这与您的其他问题相同:Do loops and while loops

您需要使用if语句或switch语句来解析用户的输入:

if (response == 'A')
{
    Console.WriteLine("Enter amount of sales");
    string salesStr = Console.ReadLine();
    Console.WriteLine(Double.Parse(salesStr) * COMMRATE);
    Console.WriteLine("Enter sales member to continue or Z to exit");
    response = Convert.ToChar(Console.ReadLine());
}

if (response == 'B')
{
    Console.WriteLine("Enter amount of sales");
    string salesStr = Console.ReadLine();
    Console.WriteLine(Double.Parse(salesStr) * COMMRATE);
    Console.WriteLine("Enter sales member to continue or Z to exit");
    response = Convert.ToChar(Console.ReadLine());
}

if (response == 'E')
{
    Console.WriteLine("Enter amount of sales");
    string salesStr = Console.ReadLine();
    Console.WriteLine(Double.Parse(salesStr) * COMMRATE);
    Console.WriteLine("Enter sales member to continue or Z to exit");
    response = Convert.ToChar(Console.ReadLine());
}

此外,您在salesStr范围内(或在我的示例中为while)声明字符串if。您将无法在其他任何地方访问它。由于你在每个块中重复相同的逻辑,但是具有不同的值,我倾向于将重复代码移到它之外,并且只将它放在那里一次。这样,如果需求发生变化,您只需要在将来更改一个位置:

string salesStr;

if (response == 'A')
{
    Console.WriteLine("Enter amount of sales");
    salesStr = Console.ReadLine();
}

if (response == 'B')
{
    Console.WriteLine("Enter amount of sales");
    salesStr = Console.ReadLine();
}

if (response == 'E')
{
    Console.WriteLine("Enter amount of sales");
    salesStr = Console.ReadLine();
}

Console.WriteLine(Double.Parse(salesStr) * COMMRATE);
Console.WriteLine("Enter sales member to continue or Z to exit");
response = Convert.ToChar(Console.ReadLine());

由于这些if语句中的每一个都做同样的事情,你也可以将它们组合起来:

string salesStr;

if (response == 'A' || response == 'B' || response == 'E')
{
    Console.WriteLine("Enter amount of sales");
    salesStr = Console.ReadLine();
}

Console.WriteLine(Double.Parse(salesStr) * COMMRATE);
Console.WriteLine("Enter sales member to continue or Z to exit");
response = Convert.ToChar(Console.ReadLine());

答案 2 :(得分:1)

查看程序流入的顺序:首先,它检查As,然后检查Bs,然后检查Es,然后退出。

看起来你还没有对销售人员的身份做任何重要事情,虽然我假设你以后会这样做。

而不是三个单独的循环,只要响应不是Z,就需要一个大循环,然后在内部使用一组if(...)语句,或者甚至是switch语句,决定如何处理销售人员的身份。