您的位置:首页 > 编程语言 > C#

【C#基础】【语法04】课后作业题——C#第一阶段测试题

2014-08-27 17:45 549 查看
C#第一阶段测试

一、 选择题

1、以下关于C#代码的说法正确的是( )。(选择一项)

for (int i = 1; i <= 3; i++)
{
switch (i)
{
case 1:
Console.Write(i.ToString());
case 2:
Console.Write((i * 2).ToString());
case 3:
Console.Write((i * 3).ToString());
}
}


a) 程序将报告编译错误,提示Case标签不能贯穿到另一个标签,不能运行

b) 运行是输出149

c) 运行时输出123246369

d) 运行时在屏幕上没有输出

2、在C#中,下列代码的运行结果是( )。(选择一项)

int[] age = new int[] { 16, 18, 20, 14, 22 };
foreach (int i in age)
{
if (i > 18)
continue;
Console.WriteLine(i.ToString() + " ");
}


a) 16 18 20 14 22

b) 16 18 14 22

c) 16 18 14

d) 16 18

3、在C#中定义一个数组,正确的代码为( )。(选项一项)

a) int arraya = newint[5];

b) int[] arraya = new int[5];

c) int arraya = new int;

d) int[5] arraya = new int;

4、在C#中,下列代码的运行结果是( )。(选择一项)

</pre><pre name="code" class="csharp">static void Main(string[] args)
{
Console.WriteLine("运行结果是:{0}","运行结果");
}


a) 运行结果

b) 运行结果是:{0}

c) 运行结果是:运行结果

d) 运行结果是运行结果

5、在C#中,下列代码的运行结果是( )。(选择一项)

string[] names = {"ACCP","BENET","BETEST"};
foreach(string name in names)
{
Console.WriteLine(name);
}


a) ACCP

BENET

BETEST

b) n

a

m

e

s

c) name

name

name

d) “ACCP”

“BENET”

BETEST”

6、在C#中,下列变量定义与赋值正确的是( )。 (选择一项)

a) int age = 12.5;

b) string name = ACCP;

c) double money = 100.0;

d) char level = A;

7、在C#中,下列代码运行后,变量Max的值是( )。(选择一项)

<span style="white-space:pre"></span>
<span style="white-space:pre">	</span>    int a = 5, b = 10, c = 15, Max = 0;
Max = a > b ? a : b;
Max = c < Max ? c : Max;


a) 0
b) 5 c) 10
d) 15

8、运行如下的C#代码将( )。(选择一项)

<span style="white-space:pre">	</span>    int a=10;
string b="10";
Console.WriteLine(a+b);


a) 输出:20

b) 输出:1010

c) 输出:10+10

d) 编译报错,提示数据类型不匹配

9、运行如下C#代码

<span style="white-space:pre">	</span>    string str = "hello-accp";
string newStr = str.Substring(1, 5);


最后得到的newStr的值为( )。(选择一项)

a) hello

b) ello-a

c) hello-accp

d) ello-

10、要获得字符串String str=”hello”的长度,需要使用( )。(选择一项)

a) str.Length

b) str.Length()

c) str.Count

d) str.Lengths

二、 填空题

1、C#程序的源文件的扩展名是

2、在控制台应用程序中,进行输入输出操作要用到System命名空间的 类。

3、如果想去掉字符串两端的空格,需要使用方法

4、while循环与do-while循环的区别是

5、break语句的作用是

6、continue语句的作用是

7、自定义方法Show(),如果想其他类也能访问到该方法,那么他的访问修饰符应该设置为

8 、Console.ReadLine()方法的返回值类型是

9、访问数组需要使用下标,问数组的下标取值是从

10、请完善代码,实现程序接收用户输入的年龄

int age=




三、 编程题

1、在Main方法中,编写一个程序用来计算长方形的面积。(要求:长与宽从键盘输入)

2、在Main方法中,编写一个程序,实现两个整数的四则运算。(提示:使用switch)

3、在Main方法中,求整数1~100的累加值,要求跳过所有个位为3的数。(提示:使用for循环)

4、在Main方法中,打印九九乘法口诀表。(提示:使用嵌套循环实现)

5、根据用户输入的姓名,返回姓名的长度。

要求:自定义方法实现,有参有返回(传入姓名,返回长度)

6、自定义方法实现获得系统当前时间,要求无参有返回。(返回当前时间DateTime)

7、判断一个数是否是素数。(素数只能被1和本身整除的数)

要求:自定义方法实现,有参有返回(传入一个数,返回布尔类型)

8、求最小公倍数。

要求:自定义方法实现,有参有返回(传入两个数,返回最小公倍数)

9、在Main方法中,编写代码求出数组

int[] nums=new int[5]{23,13,45,18,56};的最大值。

10、在Main方法中,编写代码实现对数组

int[] nums=new int[5]{23,13,45,18,56};的冒泡排序。

自己的答案,不知道对错。

选择题: ACBCA
CCBDA

填空题: 1、.cs
2、 3、Trim
4、前者是先判断再执行,后者是先循环一次再判断。

5、跳出循环
6、进入下一循环 7、public 8、字符串
9、0到数组长度-1

10、int.parse(Console.Readline());

编程题:

namespace 第一题
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("请输入矩形的长和宽来计算其面积:");
Console.WriteLine("矩形的长为:");
int w = int.Parse(Console.ReadLine());
Console.WriteLine("矩形的宽为:");
int h = int.Parse(Console.ReadLine());
Console.WriteLine();
int area = w * h;
Console.WriteLine("矩形的面积为:{0}",area);
Console.ReadKey();
}
}
}


namespace 第二题
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("实现两个整数的四则运算:");
Console.WriteLine("请分别输入两个整数:");
int a = int.Parse(Console.ReadLine());
int b = int.Parse(Console.ReadLine());
Console.WriteLine();
Console.WriteLine("请输入要操作的运算符(‘+’‘-’‘*’‘/’):");
string fu = Console.ReadLine();
int sum = 0;
switch(fu)
{
case "+":
sum = a + b;
break;
case "-":
sum = a - b;
break;
case "*":
sum = a * b;
break;
case "/":
sum = a / b;
break;
default:
Console.WriteLine("输入运算符错误!");
break;
}
Console.WriteLine("运算结果为:{0}{1}{2}={3}。",a,fu,b,sum);
Console.ReadKey();
}
}
}


namespace 第三题
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("计算1~100的累加和,并跳过所有个位为3的数:");
//引入求和sum
int sum = 0;
for (int i = 1; i <= 100; i++)
{
if (i % 10 == 3)
{
continue;
}
sum += i;
}
Console.WriteLine("求得结果为:"+sum);
Console.ReadKey();
}
}
}


namespace 第四题
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("打印九九乘法表:");
Console.WriteLine();
for (int i = 1; i <= 9; i++)
{
for (int j = 1; j <= i; j++)
{
Console.Write("{0}X{1}={2}\t",j,i,i*j);
}
Console.WriteLine();
}
Console.ReadKey();
}
}
}


namespace 第五题
{
class Program
{
static void Main(string[] args)
{
Class1 chang = new Class1();
Console.WriteLine("通过输入姓名返回姓名长度:");
Console.WriteLine("请输入您的姓名:");
string name = Console.ReadLine();
int s = chang.GetLength(name);
Console.WriteLine("您的姓名长度为:"+s);

}
}
class Class1
{
public int GetLength(string name)
{
return name.Length;
}
}
}


namespace 第六题
{
class Program
{
static void Main(string[] args)
{
Class1 time = new Class1();
string timeout = time.add().ToString();
Console.WriteLine("当前系统时间为:"+timeout);
}
}
class Class1
{
public DateTime add()
{
DateTime currentime = DateTime.Now;
return currentime;
}
}
}


namespace 第七题
{
class Program
{
static void Main(string[] args)
{
Class1 s = new Class1();
Console.WriteLine("判断一个数是否为素数:");
Console.WriteLine("请输入一个正整数:");
int num = int.Parse(Console.ReadLine());
Console.WriteLine();

bool flag=s.add(num);
if (flag == false)
{
Console.WriteLine("该数不是素数!");
}
else
Console.WriteLine("该数是素数。");
Console.ReadKey();
}
}

class Class1
{
public bool add(int num)
{
//假设该数是素数
bool flag = true;
for (int i = 2; i <= num / 2; i++)
{
if (num % i == 0)
{
flag = false;
break;
}
}
return flag;
}
}
}


namespace 第八题
{
class Program
{
static void Main(string[] args)
{
Class1 s = new Class1();
Console.WriteLine("求两个数的最小公倍数:");
int a = int.Parse(Console.ReadLine());
int b = int.Parse(Console.ReadLine());
int temp = s.add(a, b);
Console.WriteLine("最小公倍数:" + temp.ToString());
}
}

class Class1
{
public int add(int a, int b)
{
int temp = 0;
for (int i = 1; i <= b; i++)
{
if (a * i % b == 0)
{
temp = a * i;
break;
}
}
return temp;
}
}
}


namespace 第九题
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("求数组最大值:");
int[] nums=new int[5]{23,13,45,18,56};
int max = nums[0];
foreach (char c in nums)
{
if (max < c)
max = c;
}
Console.WriteLine("该数组中最大值为:"+max);
}
}
}


namespace 第十题
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("对数组进行冒泡排序:");
int[] nums = new int[5] { 23, 13, 45, 18, 56 };
Console.WriteLine("排序前数组为:");
foreach (int a in nums)
{
Console.Write(a+"\t");
}
Console.WriteLine();

//引入交换变量temp
int temp;
for (int i = 0; i < 4; i++)
{
for (int j = 0; j < 4 - i; j++)
{
if (nums[j] > nums[j + 1])
{
temp = nums[j];
nums[j] = nums[j + 1];
nums[j + 1] = temp;
}
}
}
Console.WriteLine("排序后数组为:");
foreach (int b in nums)
{
Console.Write(b+"\t");
}
Console.WriteLine();
}
}
}


并非标准答案。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: