C# 如何为错误添加异常(try、catch)

C# 如何为错误添加异常(try、catch),c#,C#,这是我的一些控制台应用程序代码。第一件不太好用的事情是方法转换。当我输入一些字符串并按enter键时,每个字母都会转到新行。我不知道为什么。另外,我不知道如何在PrintBeforeSwitch和PrintAfterSwitch方法中显示小写和大写。以及异常如何对某些异常使用try和catch以及使用哪个异常 class Program { static void Main(string[] args) { Console.WriteLine("Please inpu

这是我的一些控制台应用程序代码。第一件不太好用的事情是方法转换。当我输入一些字符串并按enter键时,每个字母都会转到新行。我不知道为什么。另外,我不知道如何在PrintBeforeSwitch和PrintAfterSwitch方法中显示小写和大写。以及异常如何对某些异常使用try和catch以及使用哪个异常

class Program
{
   static void Main(string[] args)
   {
        Console.WriteLine("Please input string");
        string input = Console.ReadLine();

        NewString ns = new NewString();
        StringOperation so = new StringOperation();

        ns.SwitchLetters(input);
        so.PrintBeforeSwitch(input);
        so.PrintAfterSwitch(input);   
    }
}

class NewString
{
    private string newString;

    public string _NewString
    {
        get
        {
            return newString;
        }
        set
        {
            newString = value;
        }
    }
    public void SwitchLetters(string newStr)
    {
        StringBuilder myString = new StringBuilder();

        for (int i = 0; i < newStr.Length; i++)
        {
            if (char.IsUpper(newStr, i))
                myString.Append(char.ToLower(newStr[i]));
            else if (char.IsLower(newStr, i))
                myString.Append(char.ToUpper(newStr[i]));
            else
                myString.Append(newStr[i]);

            Console.WriteLine(myString.ToString());
            Console.ReadLine();
            Console.WriteLine(newStr.ToUpper());
        }

    }

}

class StringOperation
{
    private string inputString;
    //public NewString newSrt;

    public string InputString
    {
        get
        {
            return inputString;
        }
        set
        {
            inputString = value;
        }
    }

    public int VocalNumber(string str)
    {
        int num; 
        return num = str.Count(a => "aeiouAEIOU".Contains(a));
    }

    public int SpaceNumber(string str)
    {
        int num;
        return num = str.Count(b => b == ' ');
    }

    public List<int> LowerUpperCaseLattersNumber(string str)
    {
        int counterLower = 0;
        int counterUpper = 0;
        List<int> counter = new List<int>();
        foreach (char value in str)
        {
            if (char.IsLower(value))
            {
                counterLower++;
            }
            else
            {
                counterUpper++;
            }
        }

        counter.Add(counterLower);
        counter.Add(counterUpper);
        Console.WriteLine("Number of small latters is: {0}", counterLower);
        Console.WriteLine("Number of big letters is: {0}", counterUpper);
        return counter;
    }

    public string SwitchVocals(ref string str)
    {
        string vocals = str.Replace("a", "x").Replace("e", "x").Replace("i", "x").Replace("o", "x").Replace("u", "x").Replace("A", "X").Replace("E", "X").Replace("I", "X").Replace("O", "X").Replace("U", "X");
        Console.WriteLine(vocals);
        return vocals;
    }

    public void PrintBeforeSwitch(string str)
    {

        Console.WriteLine(str);
        Console.WriteLine("Information about string:");
        Console.WriteLine("Number of vowels: {0}", VocalNumber(str));
        Console.WriteLine("Number of space: {0}", SpaceNumber(str));
        Console.WriteLine("Number of small latters is: {0}", LowerUpperCaseLattersNumber(str));
        Console.WriteLine("Number of big letters is: {0}", LowerUpperCaseLattersNumber(str));
        Console.ReadLine();
    }

    public void PrintAfterSwitch(string str)
    {
        SwitchVocals(ref str);
        Console.WriteLine("Information about string after switch:");
        Console.WriteLine("Number of vowels: {0}", VocalNumber(str));
        Console.WriteLine("Number of space: {0}", SpaceNumber(str));
        Console.WriteLine("Number of small latters is: {0}", LowerUpperCaseLattersNumber(str));
        Console.WriteLine("Number of big letters is: {0}", LowerUpperCaseLattersNumber(str));
        Console.ReadLine();
    }
 }
类程序
{
静态void Main(字符串[]参数)
{
Console.WriteLine(“请输入字符串”);
字符串输入=Console.ReadLine();
NewString ns=新的NewString();
StringOperation so=新的StringOperation();
ns.开关字母(输入);
so.PrintBefore开关(输入);
so.PrintAfterSwitch(输入);
}
}
类新闻字符串
{
私有字符串新闻字符串;
公共字符串\u新闻字符串
{
收到
{
返回新闻字符串;
}
设置
{
newString=值;
}
}
公共信函(字符串newStr)
{
StringBuilder myString=新建StringBuilder();
for(int i=0;iaeiouAEIOU.Contains(a));
}
公共整数空格号(字符串str)
{
int-num;
returnnum=str.Count(b=>b='');
}
公共列表LowerUpperCaseLattersNumber(字符串str)
{
int计数器=0;
int计数器上限=0;
列表计数器=新列表();
foreach(str中的字符值)
{
if(字符IsLower(值))
{
计数器++;
}
其他的
{
计数器上层++;
}
}
计数器。添加(计数器下降);
添加(台面);
WriteLine(“小车床的数量为:{0}”,计数器);
WriteLine(“大字母的数量为:{0}”,计数器上方);
返回计数器;
}
公共字符串切换人声(参考字符串str)
{
弦乐人声=str.Replace(“a”,“x”)。Replace(“e”,“x”)。Replace(“i”,“x”)。Replace(“o”,“x”)。Replace(“u”,“x”)。Replace(“a”,“x”)。Replace(“e”,“x”)。Replace(“o”,“x”)。Replace(“o”,“x”)。Replace(“u”,“x”);
主控台。编剧线(人声);
回音;
}
public void printforeswitch(字符串str)
{
控制台写入线(str);
WriteLine(“关于字符串的信息:”);
WriteLine(“元音的数量:{0}”,VocalNumber(str));
WriteLine(“空格数:{0}”,空格数(str));
WriteLine(“小latter的数量为:{0}”,LowerUpperCaseLattersNumber(str));
WriteLine(“大写字母的数量为:{0}”,LowerUpperCaseLattersNumber(str));
Console.ReadLine();
}
公共无效PrintAfterSwitch(字符串str)
{
切换人声(ref-str);
WriteLine(“关于开关后字符串的信息:”);
WriteLine(“元音的数量:{0}”,VocalNumber(str));
WriteLine(“空格数:{0}”,空格数(str));
WriteLine(“小latter的数量为:{0}”,LowerUpperCaseLattersNumber(str));
WriteLine(“大写字母的数量为:{0}”,LowerUpperCaseLattersNumber(str));
Console.ReadLine();
}
}

以下是如何处理异常的方法:

public void SwitchLetters(string newStr)
{
    try
    {

    }
    catch(Exception ex)
    {
        Console.WriteLine(ex.ToString());
        Console.ReadLine();//this stops the program so you can read the error
    }

}
我想你把这两行交换了,所以。PrintBeforeSwitch和ns。SwitchLetters

static void Main(string[] args)
{
    Console.WriteLine("Please input string");
    string input = Console.ReadLine();

    NewString ns = new NewString();
    StringOperation so = new StringOperation();

    so.PrintBeforeSwitch(input);
    ns.SwitchLetters(input);
    so.PrintAfterSwitch(input);   
}
如果您使用
Console.WriteLine(“此处的某些字符串”)它将完全按照它所说的去做,写一行。但是,您可以使用
Console.Write(“这里有一些字符串”)并应将文本/字符串添加到现有行上

你能告诉我们更多的信息吗?你的意思是“第一件不能很好地工作的事情是方法转换字母…”它应该怎么做?它目前做什么

要解决下面评论中的问题,请删除
Console.ReadLine()

public void SwitchLetters(字符串newStr)
{
StringBuilder myString=新建StringBuilder();
for(int i=0;i
我建议在一篇帖子中尽量少问问题,这可以让用户更容易回答,让你更容易根据当前结果和预期结果提出特定问题没有问题:-)试着在这里阅读:这将帮助你在将来获得更好的答案。欢迎使用StackOverflowmethod SwitchLetters nedd将大写字母交换为小写字母和小写字母。当我输入一些字符串例如MYstriGN时,它们需要在mySTRIng中交换,这个方法可以工作,但每次我只需要按一次enter键就可以在新行中交换。
public void SwitchLetters(string newStr)
{
    StringBuilder myString = new StringBuilder();

    for (int i = 0; i < newStr.Length; i++)
    {
        if (char.IsUpper(newStr, i))
            myString.Append(char.ToLower(newStr[i]));
        else if (char.IsLower(newStr, i))
            myString.Append(char.ToUpper(newStr[i]));
        else
            myString.Append(newStr[i]);

        Console.WriteLine(myString.ToString());
        //Console.ReadLine(); This line needs to be removed
        Console.WriteLine(newStr.ToUpper());
    }

}