将用户输入的数据中的字符转换为大写

Posted

技术标签:

【中文标题】将用户输入的数据中的字符转换为大写【英文标题】:Converting Char to Uppercase From User Inputted Data 【发布时间】:2014-04-19 04:53:37 【问题描述】:

我正在尝试为用户输入一个字符(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;
 

【问题讨论】:

String 类有一个重载方法来比较不区分大小写模式下的文本,但我不确定它是否存在于 Char,我现在没有方便的 IDE。所以我建议你使用一个字符串变量作为房间类型。 是什么语言? c++? 为什么不使用string 类型,而不是char 使用 .ToUpper();这可以完成任务。 【参考方案1】:

尝试默认的ToUpper 方法。

roomtype = Char.ToUpper(roomtype);

通过这个http://msdn.microsoft.com/en-us/library/7d723h14%28v=vs.110%29.aspx

【讨论】:

【参考方案2】:
roomtype = Char.ToUpper(roomtype);

【讨论】:

【参考方案3】:
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();

【讨论】:

感谢一百万!我不敢相信我犯了一个多么简单的错误!我写道: char.ToUpper(房间类型);而不是:roomtype = char.ToUpper(roomtype);

以上是关于将用户输入的数据中的字符转换为大写的主要内容,如果未能解决你的问题,请参考以下文章

实现字符串大小写的转换

用正则表达式将字符串中的小写转换成大写,大写转换成小写,整个字符串都变成大写或是小写,怎么实现啊?

字母转换ascii码

C语言截取字符串将大写字母转换为小写字母。

java怎样将大写字母转换成小写字母

Java输入一个字符串,将其中的大写字母转换为小写字母,小写字母转换为大写字母后输出?