2014-03-14 57 views
3

我想創建一個酒店的用戶輸入一個字符(S,D或L),並應該與代碼進一步下來的程序線。我需要幫助轉換用戶輸入(無論他們輸入它的方式)以將其轉換爲大寫,以便我可以使用if語句來執行我需要的操作。 到目前爲止我的代碼如下:從用戶輸入的數據轉換爲大寫字符

public static void Main() 
{ 
    int numdays; 
    double total = 0.0; 
    char roomtype, Continue; 

    Console.WriteLine("Welcome to checkout. We hope you enjoyed your stay!"); 

    do 
    { 
     Console.Write("Please enter the number of days you stayed: "); 
     numdays = Convert.ToInt32(Console.ReadLine()); 

     Console.WriteLine("S = Single, D = Double, L = Luxery"); 
     Console.Write("Please enter the type of room you stayed in: "); 
     roomtype = Convert.ToChar(Console.ReadLine()); 


    **^Right Her is Where I Want To Convert To Uppercase^** 

     total = RoomCharge(numdays,roomtype); 
     Console.WriteLine("Thank you for staying at our motel. Your total is: {0}", total); 

     Console.Write("Do you want to process another payment? Y/N? : "); 
     Continue = Convert.ToChar(Console.ReadLine()); 


    } while (Continue != 'N'); 

    Console.WriteLine("Press any key to end"); 
    Console.ReadKey(); 
} 

public static double RoomCharge(int NumDays, char RoomType) 
{ 
    double Charge = 0; 

    if (RoomType =='S') 
     Charge = NumDays * 80.00; 

    if (RoomType =='D') 
     Charge= NumDays * 125.00; 

    if (RoomType =='L') 
     Charge = NumDays * 160.00; 

    Charge = Charge * (double)NumDays; 
    Charge = Charge * 1.13; 

    return Charge; 
} 
+1

String類有一個重載的方法在不區分大小寫模式比較文本,但我不知道它是否存在字符,我沒有一個IDE方便,現在。所以我建議你爲roomtype使用一個字符串變量。 – Raj

+0

它是什麼語言? C++? – brokenfoot

+0

爲什麼不使用'string'類型而不是'char'? –

回答

2
roomtype = Char.ToUpper(roomtype); 
0
public static void Main() 
{ 
int numdays; 
double total = 0.0; 
char roomtype, Continue; 

Console.WriteLine("Welcome to checkout. We hope you enjoyed your stay!"); 

do 
{ 
    Console.Write("Please enter the number of days you stayed: "); 
    numdays = Convert.ToInt32(Console.ReadLine()); 

    Console.WriteLine("S = Single, D = Double, L = Luxery"); 
    Console.Write("Please enter the type of room you stayed in: "); 
    roomtype = Convert.ToChar(Console.ReadLine()); 
    roomtype = Char.ToUpper(roomtype); 

    total = RoomCharge(numdays,roomtype); 
    Console.WriteLine("Thank you for staying at our motel. Your total is: {0}", total); 

    Console.Write("Do you want to process another payment? Y/N? : "); 
    Continue = Convert.ToChar(Console.ReadLine()); 


} while (Continue != 'N'); 

Console.WriteLine("Press any key to end"); 
Console.ReadKey(); 
} 
+0

非常感謝!我無法相信我犯了多麼簡單的錯誤!我寫道: – user3418316

+0

char.ToUpper(roomtype);而不是:roomtype = char.ToUpper(roomtype); – user3418316

相關問題