2010-09-09 49 views
0

如果用戶輸入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()); 
     } 

回答

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") 
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()); 
+0

我有同樣的問題。我可以從A開始,然後轉到B然後E,但是當我嘗試再次擊中A時,程序關閉。 – user770022 2010-09-09 04:42:52

+0

@ randywhite30 - 檢查Val上面的答案,它包含循環以保持程序運行,直到您按下Z. – 2010-09-09 04:48:50

+0

+1爲隨附的課程計劃。 @randy你沒有循環你的程序,你正在使用if語句這樣的循環。 – Val 2010-09-09 04:50:04

1

看按照程序流入的順序:首先,它檢查As,然後檢查Bs,然後Es,然後退出。

看起來你對銷售人員的ID還沒有做任何重要的事情,儘管我認爲你會遲一些。

而不是三個單獨的循環,只要響應不是Z,就需要一個大循環,然後在內部使用一組if(...)語句,或者甚至使用switch語句來決定如何處理銷售員的ID。