循环语句的理解

来源:互联网 发布:周恩来下神坛知乎 编辑:程序博客网 时间:2024/05/18 00:34

for循环的基本应用,更重要的是对逻辑的思维逻辑能力:(该方法是较好的利用了for循环语句)

namespace  将输入的字符串逆向输出并添加“”号!
{
    class Program
    {
        static void Main(string[] args)
        {
            string[] chuan=new string[5];
            Console.WriteLine("请输入:");
            for (int i=0;  i<=4; i++)
            {               
              chuan[i]=Console.ReadLine();               
            }
            for (int a = 4; a >= 0;a-- )
            {
                chuan[a] = chuan[a].Insert(chuan[a].Length, "\"");
                chuan[a] = chuan[a].Insert(0, "\"");
                Console.WriteLine(chuan[a]);
            }
             Console.ReadLine();
        }
    }
}
更好的利用利用循环语句可以让你省去难懂的逻辑,(看起来虽然麻烦,但是理解起来更加容易)

namespace 删除输入字符串中的s,t字母!
{
    class Program
    {
        static void Main(string[] args)
        {
             Console.WriteLine("请输入字符串");
             string st = Console.ReadLine();
             bool f = true;
             while (f)
             {
                 if (st.IndexOf("s") != -1  )
                 {
                     st= st.Remove(st.IndexOf("s"), 1);
                    
                 }
                 if(st.IndexOf("t") != -1)
                 {
                     st = st.Remove(st.IndexOf("t"), 1);
                 }
                 if (st.IndexOf("s") == -1 && st.IndexOf("t") == -1)
                 {
                     f = false;
                 }                 
             }
             Console.WriteLine(st);
             Console.ReadLine();
        }
    }
}
所以,要不增强自身的逻辑判断能力,要不就多加练习,把循环语句运用得心应手!
 

 

原创粉丝点击