zoukankan      html  css  js  c++  java
  • c#练习题

    练习1:提示用户输入用户名,然后再提示输入密码,如果用户名是“admin”并且密码是“888888”,则提示正确,否则提示错误,如果用户名不是admin还提示用户用户名不存在。

    练习1

    练习2:提示用户输入年龄,如果大于等于18,则告知用户可以查看,如果小于10岁,则告知不允许查看,如果大于等于10岁,则提示用户是否继续查看(yes、no),如果输入的是yes则提示用户可以查看,否则提示不可以查看。(给出测试用例。)

     1 练习2练习
     2 using System;
     3 using System.Collections.Generic;
     4 using System.Linq;
     5 using System.Text;
     6 
     7 /*
     8  练习2:提示用户输入年龄,
     9  * 如果大于等于18,则告知用户可以查看,
    10  * 如果小于10岁,则告知不允许查看,
    11  * 如果大于等于10岁,则提示用户是否继续查看(yes、no),
    12  * 如果输入的是yes则提示用户可以查看,否则提示不可以查看。
    13  * (给出测试用例。)
    14  */
    15 namespace ConsoleApplication1
    16 {
    17     class Program
    18     {
    19         static void Main(string[] args)
    20         {
    21             int age;//声明一个年龄变量
    22 
    23             //提示用户输入年龄
    24             Console.WriteLine("请输入您的年龄:");
    25 
    26             //接收用户的输入(接收的是string类型变量,需要转换为int类型)
    27             //下面两个都可以进行转换
    28             //age =int.Parse(Console.ReadLine());
    29             age = Convert.ToInt32(Console.ReadLine());
    30 
    31             //对年龄进行判断,大于18岁(显示可以查看),小于10岁(显示不允许查看)
    32             if (age >= 18)
    33             {
    34                 Console.WriteLine("可以查看内容!");
    35             }
    36             else
    37             { 
    38                 //判断年龄是否大于等于10
    39                 if (age >= 10)
    40                 {
    41                     string s;//声明接收变量
    42 
    43                     //给出提示:是否继续查看yes/no
    44                     Console.WriteLine("是否继续查看? yes/no");
    45 
    46                     s = Console.ReadLine();//接收用户输入
    47                     //进行判断
    48                     if (s == "yes")
    49                     {
    50                         Console.WriteLine("可以查看内容!");
    51                     }
    52                     else
    53                     {
    54                         Console.WriteLine("不可以查看!");
    55                     }
    56                 }
    57                 else
    58                 {
    59                     Console.WriteLine("不可以查看!");
    60                 }
    61             }
    62 
    63             //会暂停一下,按任意键关闭窗口
    64             Console.ReadKey();
    65         }
    66     }
    67 }
    练习2

    练习3:从一个整数数组中取出最大的整数

     1 using System;
     2 using System.Collections.Generic;
     3 using System.Linq;
     4 using System.Text;
     5 
     6 /*
     7  练习3:从一个整数数组中取出最大的整数
     8  * (为加点难度,数组中的数据为用户输入)
     9  * 第一步:创建一个数组变量,并设置数组长度
    10  *         提示用户输入
    11  * 第二步:判断用户输入的是不是整数数值类型,不是的显示输入错误!
    12  * 第三步:for循环,取出最大值 (清屏显示)
    13  */
    14 namespace ConsoleApplication1
    15 {
    16     class Program
    17     {
    18         static void Main(string[] args)
    19         {
    20             int[] aList=new int[5];//声明一个整型数组变量
    21             int s = 0;//声明最大整数接收变量
    22             bool bl=true;//判断是否有输入错误
    23 
    24             //提示用户输入
    25             Console.WriteLine("请输入5个整数数值");
    26 
    27             //接收用户的输入(采用for循环)
    28             for (int i = 0; i < 5; i++)
    29             {
    30                 Console.WriteLine("请输入第{0}个数:",i+1);
    31                 //接收用户输入
    32                 string a = Console.ReadLine();
    33 
    34                 Console.Clear();//清屏
    35 
    36                 //判断是不是整数类型
    37                 try
    38                 {
    39                     aList[i] = int.Parse(a);
    40                     bl = true;
    41                 }
    42                 catch (Exception)
    43                 {
    44                     Console.Write("输入有误!");
    45                     bl = false;
    46                     break;
    47                 }
    48             }
    49             if(bl != false)
    50             {
    51                 max(aList);
    52             }
    53             //会暂停一下,按任意键关闭窗口
    54             Console.ReadKey();
    55         }
    56 
    57         //判断最大值
    58         public static void max(int[] a)
    59         {
    60             int s = 0;//声明最大整数接收变量
    61 
    62             for (int i = 0; i < 5; i++)
    63             {
    64                 if (s < a[i])
    65                 {
    66                     s = a[i];
    67                 }
    68             }
    69             Console.WriteLine("最大整数是:{0}", s);
    70         }
    71     }
    72 }
    练习3

    练习4:计算一个整数数组的所有元素的和。

     1 using System;
     2 using System.Collections.Generic;
     3 using System.Linq;
     4 using System.Text;
     5 
     6 /*
     7  练习4:计算一个整数数组的所有元素的和。(我直接在练习3中修改)
     8  * (为加点难度,数组中的数据为用户输入)
     9  * 第一步:创建一个数组变量,并设置数组长度
    10  *         提示用户输入
    11  * 第二步:判断用户输入的是不是整数数值类型,不是的显示输入错误!
    12  * 第三步:使用(foreach)遍历数值相加求和
    13  */
    14 namespace ConsoleApplication1
    15 {
    16     class Program
    17     {
    18         static void Main(string[] args)
    19         {
    20             int[] aList=new int[5];//声明一个整型数组变量
    21             int s = 0;//声明最大整数接收变量
    22             bool bl=true;//判断是否有输入错误
    23 
    24             //提示用户输入
    25             Console.WriteLine("请输入5个整数数值");
    26 
    27             //接收用户的输入(采用for循环)
    28             for (int i = 0; i < 5; i++)
    29             {
    30                 Console.WriteLine("请输入第{0}个数:",i+1);
    31                 //接收用户输入
    32                 string a = Console.ReadLine();
    33 
    34                 Console.Clear();//清屏
    35 
    36                 //判断是不是整数类型
    37                 try
    38                 {
    39                     aList[i] = int.Parse(a);
    40                     bl = true;
    41                 }
    42                 catch (Exception)
    43                 {
    44                     Console.Write("输入有误!");
    45                     bl = false;
    46                     break;
    47                 }
    48             }
    49             if(bl != false)
    50             {
    51                 sum(aList);
    52             }
    53             //会暂停一下,按任意键关闭窗口
    54             Console.ReadKey();
    55         }
    56 
    57         //求和
    58         public static void sum(int[] a)
    59         {
    60             int s = 0;//声明和的接收变量
    61 
    62             foreach(int b in a)
    63             {
    64                 s += b;
    65             }
    66             Console.WriteLine("数组和是:{0}", s);
    67         }
    68     }
    69 }
    练习4

    练习5:将一个字符串数组输出为|分割的形式,比如{“浮云”,”神马”,”穿梭”}数组输出为“浮云|神马|穿梭”。不要使用String.Join等.Net内置方法。

     1 using System;
     2 using System.Collections.Generic;
     3 using System.Linq;
     4 using System.Text;
     5 
     6 /*
     7  练习5:将一个字符串数组输出为|分割的形式,
     8  * 比如{“浮云”,”神马”,”穿梭”}数组
     9  * 输出为“浮云|神马|穿梭”。
    10  * 不要使用String.Join等.Net内置方法
    11  */
    12 namespace ConsoleApplication1
    13 {
    14     class Program
    15     {
    16         static void Main(string[] args)
    17         {
    18             //声明一个数组变量
    19             string[] s=new string[]{"小明","","小红","","同桌"};
    20 
    21             //for循环遍历数组
    22             for (int i = 0; i < s.Length;i++)
    23             {
    24                 if (i < s.Length - 1)
    25                 {
    26                     Console.Write("{0} | ", s[i]);
    27                 }
    28                 else
    29                 {
    30                     Console.Write("{0}", s[i]);
    31                 }
    32             }
    33 
    34             Console.ReadKey();
    35         }
    36     }
    37 }
    练习5

    练习6:有一个整数数组,请声明一个字符串数组,将整数数组中的每一个元素的值转换为字符串保存到字符串数组中。

     1 using System;
     2 using System.Collections.Generic;
     3 using System.Linq;
     4 using System.Text;
     5 
     6 /*
     7  练习6:有一个整数数组,
     8  * 请声明一个字符串数组,
     9  * 将整数数组中的每一个元素的值(转换为字符串)
    10  * 保存到字符串数组中。
    11  */
    12 namespace ConsoleApplication1
    13 {
    14     class Program
    15     {
    16         static void Main(string[] args)
    17         {
    18             //声明并赋值一个整型数组变量
    19             int[] s=new int[]{12,34,45,67,890};
    20             //声明一个字符串数组变量
    21             string[] st=new string[s.Length];
    22             
    23             //使用for循环,循环赋值
    24             for (int i = 0; i < s.Length;i++)
    25             {
    26                 st[i] =Convert.ToString(s[i]);
    27             }
    28             Console.WriteLine("字符串数组中的值为:
    {0}{1}{2}{3}{4}",st[0],st[1],st[2],st[3],st[4]);
    29 
    30             Console.ReadKey();
    31         }
    32     }
    33 }
    练习6

    练习7:将一个字符串数组的元素的顺序进行反转。{"3","a","8","haha"} 转换为{"haha","8","a","3"}。提示:第i个和第length-i-1个进行交换。

     1 using System;
     2 using System.Collections.Generic;
     3 using System.Linq;
     4 using System.Text;
     5 
     6 /*
     7  练习7:将一个字符串数组的元素的顺序进行反转。
     8  * {"3","a","8","haha"} 转换为{"haha","8","a","3"}。
     9  * 提示:第i个和第length-i-1个进行交换。
    10  */
    11 namespace ConsoleApplication1
    12 {
    13     class Program
    14     {
    15         static void Main(string[] args)
    16         {
    17             //声明一个字符串数组,并赋值
    18             string[] st = new string[] { "3", "a", "8", "haha" };
    19             //string[] st = new string[] { "3", "a", "8", "haha" ,"qw"};
    20             string s;//声明一个中间数值
    21 
    22             //对数组长度进行判断
    23             if (st.Length % 2 == 0)
    24             {
    25                 for (int i = 0; i < st.Length / 2; i++)
    26                 {
    27                     s = st[i];
    28                     st[i] = st[st.Length -i- 1];
    29                     st[st.Length -i- 1] = s;
    30                 } 
    31             }
    32             if (st.Length % 2 == 1)
    33             {
    34                 for (int i = 0; i < ((st.Length-1) / 2); i++)
    35                 {
    36                     s = st[i];
    37                     st[i] = st[st.Length -i- 1];
    38                     st[st.Length-i - 1] = s;
    39                 }
    40             }
    41             foreach (var a in st)
    42                 {
    43                     Console.Write(" "+a);
    44                 }
    45 
    46             Console.ReadKey();
    47         }
    48     }
    49 }
    练习7
  • 相关阅读:
    图论小测
    有关连通性
    差分约束
    php中代码执行&&命令执行函数【转载】
    Boss直聘的一个csrf与url跳转漏洞(已经修复)
    Immunity Canvas初体验加操作指南
    SSRF复习版本
    XXE漏洞复习版本
    信息收集汇总
    web前端逆向初体验
  • 原文地址:https://www.cnblogs.com/pengyouqiang88/p/5019106.html
Copyright © 2011-2022 走看看