码迷,mamicode.com
首页 > Windows程序 > 详细

C#迭代语句、跳转语句--C#基础

时间:2017-09-10 14:23:16      阅读:274      评论:0      收藏:0      [点我收藏+]

标签:for   void   ons   值类型   表达   修改   ++   相互   switch   

foreach每执行一次内含的代码时,循环变量就会一次读取集合中的一个元素,不需要个数。循环变量只是一个只读的局部变量,这个值是不能修改的。char后的word是 foreach语句的迭代变量,它是只读的,不允许修改。

源程序:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace 表达式
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("请输入一串文字:");
            String str = Console.ReadLine();
            foreach (char item in str)
            {
                if (char.IsWhiteSpace(item))
                {
                    Console.WriteLine();
                }else{
                    Console.Write(item);
                      }
                  }
            Console.ReadKey();
        }
    }
}

 break跳转语句

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 using System.Threading.Tasks;
 6 
 7 namespace 表达式
 8 {
 9     class Program
10     {
11         static void Main(string[] args)
12         {
13             for (int i = 1; i < 11; i++)
14             {
15                 if (i % 4 == 0)
16                 {
17                     break;
18                 }
19 
20                 Console.Write(i);
21             }
22             Console.ReadLine();
23         }
24     }
25 }

 continue主要用于停止当前的迭代语句,结束本次循环,只能用于迭代语句

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace 表达式
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("50以内的奇数:");
            for (int i = 1; i <=50; i++)
            {
                if (i % 2 == 0) {
                    continue;
                }
                Console.Write(i+"\t");
            }
            Console.ReadKey();
        }
    }
}

 

return语句使用时有两种格式

(1)rreturn;

(2)return 表达式;

return语句只能出现在方法中,当调用方法,回到主函数。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace 表达式
{
    class Program
    {
        static void Main(string[] args)
        {
            while (true)
            {
                Console.WriteLine("请输入三个整数按回车键确认每个数的输入:");
                int a = int.Parse(Console.ReadLine());
                int b = int.Parse(Console.ReadLine());
                int c = int.Parse(Console.ReadLine());
                Console.WriteLine("平均数为{0}", average(a, b, c));
            }
            Console.ReadLine();

        }
        static double average(int A,int B,int C) {
            return (A + B + C) / 3;
        }
    }
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace 表达式
{
    class Program
    {
        static void Main(string[] args)
        {
            while (true)
            {
                Console.WriteLine("请输入三个整数按回车键确认每个数的输入:");
                int a = int.Parse(Console.ReadLine());
                int b = int.Parse(Console.ReadLine());
                int c = int.Parse(Console.ReadLine());
                average(a,b,c);
            }
            Console.ReadLine();
        }
        static void average(int A,int B,int C) {
            Console.WriteLine("平均数为{0}", (A+B+C)/3);
            return;
        }
    }
}

goto语句使用格式

goto+标识符标识符标志程序位置的方法

作用:当程序执行到goto语句时,程序会跳转到标识符所标识符所标志的位置

goto语句使用会使代码的易读性下降,在编写程序的时候尽量少用goto语句 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace 表达式
{
    class Program
    {
        static void Main(string[] args)
        {
            //5的阶层等于?
            Console.WriteLine("5的阶乘等于?根据以下选项选择正确答案,回车键确认!");
            Console.WriteLine("1. 5!=5\n2. 5!=10\n3. 5!=20\n4. 5!=60");
            //用户的判定
            //标识符标志内容
        error:
            {
                Console.WriteLine("您回答错了!");

            }
            int option=int.Parse(Console.ReadLine());
      
       
            switch(option){
                case 1:
                case 2:
                case 3: goto error;
                case 4: goto right;
                default :
                    Console.WriteLine("选项不存在,请重新选择");
                    //break;
                    goto end;
             }
             right:
             {
                Console.WriteLine("恭喜答对了");
             }
             end: ;
            //end: { }
             Console.ReadLine();
          } 
    }
}

 限制转换与隐式转换

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace 表达式
{
    class Program
    {
        static void Main(string[] args)
        {
            int x = 98;
            char y = a;
            x = y;
            y = (char)x;//强制转换
            //字符型‘a‘隐式转换为int型
            Console.WriteLine(x);//输出97
            Console.WriteLine(y);
            Console.ReadLine();
        }
    }
}

 字符串和整型数据相互转换

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace 表达式
{
    class Program
    {
        static void Main(string[] args)
        {
            /*int x = 12222;
            string str = "abcd";
            str = Convert.ToString(x);
            Console.WriteLine(str);*/
            int x = 12222;
            string str = "122222";
            x = Convert.ToInt32(str);
            Console.WriteLine(x);
            Console.Read();
        }
    }
}

 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace 表达式
{
    class Program
    {
        static void Main(string[] args)
        {
            /*int x = 12222;
            string str = "abcd";
            str = Convert.ToString(x);
            Console.WriteLine(str);*/
            int x = 12222;
            string str = "122222";
            //x = Convert.ToInt32(str);
            str = x.ToString();//值类型转换为字符串类型,可以这样用
            Console.WriteLine(x);
            Console.Read();
        }
    }
}

 

C#迭代语句、跳转语句--C#基础

标签:for   void   ons   值类型   表达   修改   ++   相互   switch   

原文地址:http://www.cnblogs.com/qikeyishu/p/7496065.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!