C# 1116 流程控制 常量

常量

csharp 复制代码
            //常量
            //不能修改,运行时
            //const
            const int achang = 12;
            const double paichang = 3.14;
            const char cchang= 'F';
            int achang1 = achang + 2;
            int bchang1 = 22;
            double abchang = paichang * bchang1;
            Console.WriteLine("achang=" + achang + ",achang1=" + achang1
                + ",abchang=" + abchang);

浮点常量

csharp 复制代码
 //整数常量
 //八进制 十进制 十六进制
 //浮点常量
 const double pi = 3.14;
 const double afudianc = 3e3;//指数形式 修正 
 double bfudianc = afudianc * 4;
 const float cfudiancf = 1.2224f;
 Console.WriteLine("bfudianc:=\'"+bfudianc+"\',cfidianc:\'"+cfudiancf+"\'");

字符常量

csharp 复制代码
//字符常量
const char czifu= 'a';
    const char dzifu = '\t';
    Console.WriteLine("优酷:\twww.mycraft.com\r\n");
    Console.WriteLine(czifu + "," + dzifu + "\\?"+"\\211"+"\r\n");

字符串常量

csharp 复制代码
  //字符串常量
  const string wangzhan = "www.mycraft.com";
  const string zifuc = @"ssadas";
  Console.WriteLine("wangzhan:\'" + wangzhan + "\',zifuchaun:" + zifuc);

流程控制

if

csharp 复制代码
namespace LiuChenPanDuan
{
    internal class Program
    {
        static void Main(string[] args)
        {
            int age = 11;
            if (age == 18) { Console.WriteLine("you are too y"); }
            else { Console.WriteLine("AGE:"+age); }
            age++;

            Console.WriteLine("Hello, World!");
        }
    }
}
csharp 复制代码
   decimal price = 1399.5m;
   if (price > 1000)
   {
       Console.WriteLine("price too high");
   } else {
       Console.WriteLine("price is normal");
}
csharp 复制代码
  //if else if
  Console.WriteLine("Please input the price");
  int priceCs = Convert.ToInt32(Console.ReadLine());
  if (priceCs < 0) {
      Console.WriteLine("wrong");
  }
  else if (priceCs >= 0 && priceCs < 200) {
      Console.WriteLine("you cannot buy anything");

  }
  else if (priceCs >=200 && priceCs <2000)
  {
          Console.WriteLine("you can buy gun,like use or " +
              "can buy smoke,flash..or clothes");
  }
  else if(priceCs >=2000 )
  {
      Console.WriteLine("YOU can buy exllent gun,like p90");
      
  }


switch

csharp 复制代码
   //switch
   Console.WriteLine("Pl input a name");
   string rename = Console.ReadLine();
   switch (rename)
   {
       case "siye":
           Console.WriteLine("classroom");
           break;
       case "faker":
           Console.WriteLine("god");
           break;
       case "":
           Console.WriteLine("5555");
           break;
       default:
           Console.WriteLine("A NAME");
           break;
   }
   Console.WriteLine("PL input a count");
   int count = 3;
   switch (count)
   {
       case 1:
           Console.WriteLine("exllent"); break;
       case 2:
           Console.WriteLine("wonderful"); break;
       case 3:
           Console.WriteLine("god"); break;
   }


for






进行条件判断,如果为 true,则执行循环主体,如果为假,则跳出 for 循环,










csharp 复制代码
    //for循环
    Console.WriteLine("pl input a str,str.length have to >5");
    string str1=Console.ReadLine();
   if (str1.Length < 5) { Console.WriteLine("str.length<5");
        return;}
    for (int i = str1.Length; i > 5; i--)
    {Console.WriteLine(str1[i-1]);
        str1 = "0" + str1;
    }
    Console.WriteLine(str1);

}
csharp 复制代码
    Console.WriteLine(str1);
    string str2 = Console.ReadLine();
    for(int i=str2.Length; i < 5; i++)
    {
        str2 = "1" + str2;
    }
    Console.WriteLine(str2);
}
csharp 复制代码
 Console.WriteLine(str2);
 string str3 = Console.ReadLine();
 for (int i = 0; i < str3.Length; i++)
 {
     if (i > 0 && i < str3.Length)
     { Console.WriteLine(""); }
     Console.WriteLine(str3[i]);

 }

乘法表

csharp 复制代码
 //for 嵌套
 for (int i = 1; i <= 9; i++)
 { for(int j = 1; j <= i; j++)
     {
         int reu = i * j;
         string result = reu.ToString().Length == 1 ? reu + "" : reu
             .ToString();
         Console.Write("{0}x{1}={2}",j,i,result);
     }
 Console.WriteLine();

无限循环

csharp 复制代码
//无限循环
for (int i = 0;;i++)
{
    if (i < 10)
    {
        Console.WriteLine("执行中");

    }
    else
        break;
}
csharp 复制代码
            int j1 = 0;
            for (int i = 0; ; i++)
            { j1++; }
csharp 复制代码
 string[] names = new string[] { "lk", "kok", "ln", "li" };
 foreach (string name in names)
 {
     Console.WriteLine("{0}", name);

 }

while

csharp 复制代码
  //while
  int iw = 1;
  int sum = 0;
  while (iw <= 100)
  {
      sum = sum + iw;
      iw++;
  }
  Console.WriteLine("1到100的和是:"+sum);
csharp 复制代码
            int i9 = 1;
            while (i9 <= 9)
            {
                int j9 = 1;
                while (j9 <= i9)
                {
                    int res = i9 * j9;
                    string result = res.ToString().Length==1?res+"":res.ToString();
                    //字符串1的长度为1吗?14长度为2吗 这里就是格式上个位(一位数)和两位数对齐吗?
                    //是
                    Console.Write("{0} x {1}={2}", j9, i9, result);
                    j9++;
}i9++;
                Console.WriteLine();      }

do while

csharp 复制代码
 //do while
 int doi = 1;
 int dosum = 0;
 do
 {
     dosum += doi;
     doi++;
 } while (doi <= 5);
 Console.WriteLine("dosum:"+dosum);
 int do9i = 1;



csharp 复制代码
   int do9i = 1;
   do
   {
       int do9j = 1;
       do
       {
           int resdo = do9i * do9j;
           string resultdo = resdo.ToString().Length == 1 ? resdo + "" : resdo.ToString();
           Console.Write("{0} x {1} = {2} " ,do9i,do9j,resultdo);
       } while (do9j <= do9i);
       do9i++;
       Console.WriteLine();
   } while (do9i <= 9);
csharp 复制代码
 int do9i = 1;
 do
 {
     int do9j = 1;
     do
     {
         int resdo = do9i * do9j;
         string resultdo = resdo.ToString().Length == 1 ? resdo + "" : resdo.ToString();
         Console.Write("{0} x {1} = {2} " ,do9i,do9j,resultdo);
         do9j++;
     } while (do9j <= do9i);
     do9i++;
     Console.WriteLine();
 } while (do9i <= 9);


break

直接跳出循环

1+...4

csharp 复制代码
  int sumb = 0;
  for (int i = 0; i < 10; i++)
  {
      if (i == 5)
          break;
      sumb += i;

  }

continue

1+...4 +6+...9

跳过余数是5的循环

csharp 复制代码
  //continue
  int sumc= 0;
  for(int i = 0;i < 10; i++)
  {
      if (i%5==0)
      {
          continue;
      }
      sumc += i;            }
  Console.WriteLine("sumc:"+sumc);
          





相关推荐
葛小白13 小时前
C#进阶12:C#全局路径规划算法_Dijkstra
算法·c#·dijkstra算法
程序定小飞3 小时前
基于springboot的汽车资讯网站开发与实现
java·开发语言·spring boot·后端·spring
大米粥哥哥4 小时前
Qt 使用QAMQP连接RabbitMQ
开发语言·qt·rabbitmq·qamqp
yivifu4 小时前
精益求精,支持处理嵌套表格的Word表格转HTML表格
开发语言·c#·word
沐知全栈开发4 小时前
PHP MySQL WHERE 子句详解
开发语言
糖纸风筝4 小时前
Java指南:eclipse、java-activemq与测试验证
java·开发语言·学习
小坏讲微服务4 小时前
整合Spring Cloud Alibaba与Gateway实现跨域的解决方案
java·开发语言·后端·spring cloud·云原生·gateway
码上成长4 小时前
<script setup> 实战模式:大型组件怎么拆?
开发语言·javascript·vue.js
九河_5 小时前
解决pip install gym==0.19.0安装失败问题
开发语言·python·pip·gym