Cshape第一次考试
基础题目
语法考察
int a = 12;
double b = 1.2;
char c = 'a';
string d = "aaa"?
string[] str = {"12"};
folat n = 0.1f;
if(true){
// 执行
}
if(false){
// 不执行
}
if(false){
} else {
执行
}
for(int i = 1;i<3;i++){
// 执行3次
}
for(int i = 3;i>-1;i--){
if(i == 0)// 执行3次
{
break;
}
}
13:while的语法?
while(bool){
// 循环体
}
14:多分支的语法结构?
if(boo){
}
else if(bool){
}
else[
]
15:switch的语法结构
switch (expression) {
case constant_expression1:
break;
case constant_expression2:
break;
default:
break;
}
16:for的语法是什么?
for(初始表达式; 条件表达式 ; 自增/自减){
}
17:创建常量的语法
const type name = vlue;
18:c#是什么类型的语言?
是一种强类型、面向对象的编译型编程语言
19:运算符 运算的优先级?
算术运算符》关系运算符和相等运算符》逻辑运算符》赋值运算符》位运算符》条件运算符(三元运算符)》空合并运算符》成员访问运算符和索引访问运算符》其他运算符
20: int a = "aaa"; 报错内容是什么?
无法将类型“string”隐式转换为“int”
api考察
一个题目一分
请写写出api
//1:获取当前时间
DateTime dt = DateTime.Now;
System.Console.WriteLine(dt);
//2:将子字符串转化int
Convert.ToInt32("string");
//3:将时间对象转化为 字符串
string timeString = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
//4:截取字符片段
string year = timeString.Substring(0, 4);
//5:验证字符串中是否存在某个字符
bool containsHyphen = timeString.Contains('-');
//6:替换字符
string modifiedTimeString = timeString.Replace('-', '/');
//7:删除字符串中某个字符串
string modifiedTimeString = timeString.Replace("-", "");
//8:字符串添加方法是什么
string modifiedTimeString = timeString.Insert(5, "INSERTED");
//9:去除首尾空格方法
string trimmedTimeString = timeString.Trim();
//10:将字母变大写
string upperCaseTimeString = timeString.ToUpper();
2:请写出下面api的作用 ,结果 参数
-
Console.ReadLine()
- 作用:从标准输入流(通常是键盘)读取下一行字符串。
- 结果:返回一个字符串,该字符串包含用户输入的一行文本。
- 参数:无。
-
String.Compare()
-
作用:比较两个指定的字符串对象在排序顺序中的相对位置。
-
结果:返回一个整数,表示两个字符串的相对顺序。
-
参数:
strA
:要比较的第一个字符串。strB
:要比较的第二个字符串。- (可选)
comparisonType
:指定比较时使用的区域设置和大小写规则。
-
-
str.Replace()
-
作用:在字符串中替换与指定值匹配的字符或字符串。
-
结果:返回一个新字符串,其中指定的旧值或旧值数组的所有匹配项都已被新值替换。
-
参数:
oldValue
:要查找并替换的字符串。newValue
:要替换为的字符串。- (可选)
startIndex
和count
:指定子字符串的起始位置和长度,仅在要搜索的字符串的特定部分内替换时才需要。
-
-
str.Remove()
-
作用:从当前字符串中移除指定位置的指定数量的字符。
-
结果:返回一个新字符串,它是此字符串的副本,但删除了指定位置处指定数量的字符。
-
参数:
startIndex
:要移除的第一个字符的零基索引。count
:要移除的字符数。
-
-
str.Insert()
-
作用:将指定的字符串插入到此实例中的指定位置。
-
结果:返回一个新字符串,它是此字符串的副本,但指定的字符串已插入到指定位置。
-
参数:
startIndex
:要插入字符串的位置的索引。value
:要插入的字符串。
-
-
str.IndexOf()
-
作用:返回指定字符串在此实例中第一次出现的索引,从指定的搜索起始位置开始。
-
结果:返回指定字符串在此实例中第一次出现的索引;如果未找到该值,则返回-1。
-
参数:
value
:要搜索的字符串。- (可选)
startIndex
:搜索的起始位置。 - (可选)
comparisonType
:指定搜索时要使用的比较类型(例如,是否区分大小写)。
-
-
str.Contains()
-
作用:确定此字符串实例是否包含指定的字符串。
-
结果:如果此字符串包含指定的字符串,则为
true
;否则为false
。 -
参数:
value
:要搜索的字符串。- (可选)
comparisonType
:指定搜索时要使用的比较类型(例如,是否区分大小写)。
-
-
str.Trim()
-
作用:从当前字符串的开始和结束位置移除所有前导和尾随空白字符。
-
结果:返回一个新字符串,它是此字符串的副本,但已删除了前导和尾随空白字符。
-
参数:
- (可选)
trimChars
:一个字符数组,其中包含要从当前字符串的开始和结束位置移除的字符。
- (可选)
-
-
str.Split()
-
作用:通过指定字符或字符串数组中的任何字符或字符串拆分此字符串实例,并返回一个子字符串数组。
-
结果:返回一个字符串数组,其中包含此字符串中的子字符串,这些子字符串由指定的字符或字符串数组中的字符或字符串分隔。
-
参数:
separator
:用于分隔子字符串的字符或字符串数组。- (可选)
options
:指定拆分操作的选项(例如,是否移除空数组元素)。
-
-
str.ToUpper()
- 作用:将字符串中的所有字符转换为大写。
- 结果:返回一个新字符串,它是此字符串的副本,但其中的所有字符都已转换为大写。
- 参数:无。
简单题目
- 翻译main中每一行代码
static void Main(string[] args)
{
// 定义一个名为fl的float类型变量,并初始化为3.14,末尾的F表示这是一个float字面量
float fl = 3.14F;
// 定义一个名为d的double类型变量,并初始化为3.14,末尾的D表示这是一个double字面量(但D在这里是可选的,因为默认double字面量不需要后缀)
double d = 3.14D;
// 将d的值强制转换为float类型,并赋值给fl
fl = (float)d;
// 定义一个名为ls的int类型变量,并初始化为927
int ls = 927;
// 定义一个名为shj的byte类型变量
//byte shj = 300; // 超出byte值(0-255)范围,编译错误
// 在控制台上输出ls的值,格式为"ls=值"
Console.WriteLine("ls={0}", ls);
// 在控制台上输出shj的值,格式为"shj=值"
Console.WriteLine("shj={0}", shj);
// 等待用户输入后继续执行(通常用于防止控制台窗口在输出后立即关闭)
Console.ReadLine();
}
2翻译main中每一行代码
static void Main(string[] args)
{
int x = 24; // 定义一个整数变量 x 并赋值为 24
x += x--;// 先使用 x 的值(24),然后 x 减 1 (x = 23),最终 x = 24 + 24 = 48
x += x %= 17; // x 对 17 取模,然后将结果加到 x 上,48 % 17 = 14,最终 x = 48 + 14 = 62
Console.WriteLine("变量x的值为:" + x); // 输出 x 的值,即 62
int y = 24; // 定义一个整数变量 y 并赋值为 24
y += --y; // 先减 1,然后使用 y 的值 (y = 23),最终 y = 24 + 24 = 48
y += y %= 17; // y 对 17 取模,然后将结果加到 y 上,46 % 17 = 12,最终 y = 48 + 12 = 60
Console.WriteLine("变量y的值为:" + y); // 输出 y 的值,即 60
Console.ReadLine(); // 等待用户输入,防止程序立即结束
}
3: 请写出下面程序的运算过程 .求res结果的执行过程
string str = "admin";
string res = "";
for (int i = 2; i < 4; i++) // 循环从 i = 2 到 i = 3
{
res = res + str[i]; // 将 str[i] 追加到 res
}
// 最终 res 的值为 "mi"
- 开始
for
循环,i
的初始值为 2。
- 当
i = 2
时,str[2]
是'm'
。将'm'
追加到res
中,res
变为"m"
。
i
增加 1,变为3
。
- 当
i = 3
时,str[3]
是'i'
。将'i'
追加到res
中,res
变为"mi"
。
-
i
增加 1,变为4
。但i
不小于 4,循环结束。 -
将鞋面代码用for方式实现
Console.WriteLine("变量x的值“);
Console.WriteLine("变量x的值“);
Console.WriteLine("变量x的值“);
for (int i = 0; i < 3; i++)
{
Console.WriteLine("变量x的值");
}
5:将下面代码用for输出
Console.WriteLine(1);
Console.WriteLine(1);
Console.WriteLine(1);
for (int i = 0; i < 3; i++)
{
Console.WriteLine(1);
}
================================================================
Console.WriteLine(1);
Console.WriteLine(2);
Console.WriteLine(3);
for (int i = 1; i <= 3; i++)
{
Console.WriteLine(i);
}
================================================================
int res = 0;
res = res + 0;
res = res + 1;
res = res + 2;
res = res + 3;
res = res + 4;
int res = 0;
for (int i = 0; i <= 4; i++) {
res = res + i;
}
================================================================
string str = "admin";
string res = "";
res = res + str[0];
res = res + str[1];
res = res + str[2];
res = res + str[4];
string str = "admin";
string res = "";
for (int i = 0; i < str.Length; i++)
{
if (i != 3) // 跳过索引为3的字符
{
res += str[i];
}
}
Console.WriteLine(res);
附加题目
1、
string str = "aaddddddeee";
求最长字符出现几次?
using System;
class Program
{
static void Main()
{
string str = "aaddddddeee";
int maxCount = 0;
int currentCount = 1;
for (int i = 1; i < str.Length; i++)
{
if (str[i] == str[i - 1])
{
currentCount++;
}
else
{
if (currentCount > maxCount)
{
maxCount = currentCount;
}
currentCount = 1;
}
}
// 检查最后一个字符序列
if (currentCount > maxCount)
{
maxCount = currentCount;
}
Console.WriteLine("最长字符出现的次数是: " + maxCount);
}
}
- 获取当前时间并用 2020-12-11 下午 12:12:12格式展示
using System;
class Program
{
static void Main()
{
// 获取当前时间
DateTime now = DateTime.Now;
// 定义时间格式
string formattedTime = now.ToString("yyyy-MM-dd tt hh:mm:ss", new System.Globalization.CultureInfo("zh-CN"));
// 输出格式化后的时间
Console.WriteLine(formattedTime);
}
}
- 正序便利字符串
string str = "aaddddddeee";
using System;
class Program
{
static void Main()
{
string str = "aaddddddeee";
int maxCount = 0;
int currentCount = 1;
for (int i = 1; i < str.Length; i++)
{
if (str[i] == str[i - 1])
{
currentCount++;
}
else
{
if (currentCount > maxCount)
{
maxCount = currentCount;
}
currentCount = 1;
}
}
// 检查最后一个字符序列
if (currentCount > maxCount)
{
maxCount = currentCount;
}
Console.WriteLine("最长字符出现的次数是: " + maxCount);
}
}
4.倒序遍历字符串
string str = "aaddddddeee";
string str = "aaddddddeee";
for (int i = str.Length - 1; i >= 0; i--)
{
Console.WriteLine(str[i]);
}
string str = "aaddddddeee";
foreach (char c in str.Reverse())
{
Console.WriteLine(c);
}
5.求不重复字符串
string str = "aaddddddeee";
using System;
using System.Collections.Generic;
class Program
{
static void Main()
{
string str = "aaddddddeee";
HashSet<char> uniqueChars = new HashSet<char>(str);
foreach (char c in uniqueChars)
{
Console.Write(c);
}
}
}
排错题目
找出下面每个代码的错误,并写出错误原因
1
输出100以内所有奇数
using System;
class Program
{
static void Main()
{
for (int i = 1; i <= 100; i++)
{
if (i % 2 != 0)
{
Console.Write(i + " ");
}
}
}
}
2
static void Main(string[] args)
{
string int= "明日科技"; //无法将类型“string”隐式转换为“int”
Console.WriteLine("字符串变量的值:" + int);
Console.ReadLine();
}
应为非关键字的其他命名,输出结果为:字符串变量的值:明日科技
3
static void Main(string[] args)
{
string str = "C#编程词典";
int price; //使用了未赋值的局部变量“price”
Console.WriteLine(str + "的价格是" + price); //该位置报错
Console.ReadLine();
}
4 找错误,并要求查看输出内容是什么?
static void Main(string[] args)
{
int i=368; //意外的字符“;”应输入 ;
Console.WriteLine(i.ToString());
Console.ReadLine();
}
中英分号区分错误,输出结果为368
5
static void Main(string[] args)
{
float fl = 15.5; //无法将 Double 类型隐式转换为“float”类型;请使用“F”后缀创建此类型
Console.WriteLine("float变量值为:" + fl);
Console.ReadLine();
}
结果为:float变量值为:15.5 6 找错误,修正后 请写出正确的输出内容是什么
static void Main(string[] args)
{
double x = 19810927.0112;
int y = x; //无法将类型“double”隐式转换为“int”。存在一个显式转换(是否缺少强制转换?)
Console.WriteLine("double变量值:" + x);
Console.WriteLine("int变量值:" + y);
Console.ReadLine();
}
输出结果为:double变量值:19810927.0112 int变量值:19810927
7
static void Main(string[] args)
{
int shj1 = 45;
int shj2 = 0;
int ls;
ls = shj1 / shj2; //System.DivideByZeroException:“尝试除以零。”
Console.WriteLine(ls.ToString());
Console.Read();
}
static void Main(string[] args)
{
char cName = "明日"; //无法将类型“string”隐式转换为“char”
Console.WriteLine(cName);
Console.ReadLine();
}
9
static void Main(string[] args)
{
string strName = "mingrikeji";
string strSName = strName.Substring(7, 4); //System.ArgumentOutOfRangeException:“索引和长度必须引用该字符串内的位置。
Arg_ParamName_Name”
Console.WriteLine("简称为:" + strSName);
Console.ReadLine();
}
10
static void Main(string[] args)
{
string str1 = null; //将 null 文本或可能的 null 值转换为不可为 null 类型。
string str2 = "";
str1.ToLower(); //“str1”可能在此处为 null。
str2.ToLower();
}