1、使用for循环和continue语句,创建一个代码片段,将1到10之间的偶数输出到控制台。
以下是满足需求的C#代码示例:
using System;
public class EvenNumberPrinter
{
static void Main()
{
for (int i = 1; i <= 10; i++)
{
if (i % 2 != 0)
{
continue;
}
Console.WriteLine(i);
}
}
}
在上述代码中,使用 for 循环遍历1到10的数字。对于每个数字,使用 if 语句检查它是否为奇数(通过取模运算符 % 判断),如果是奇数,则使用 continue 语句跳过本次循环,继续下一次循环;如果是偶数,则将其输出到控制台。
2、给定初始变量声明和赋值:int a = 1; int b = 1; int c = 1; 计算以下表达式的值:((((c++ + –a) * b) != 2) && true)
False
3、以合理的方式绘制一个抽象层次结构,关联以下所有类别:苹果、香蕉、牛肉、饮料、奶酪、可消费物、乳制品、食物、水果、青豆、肉类、牛奶、猪肉、菠菜、蔬菜。并指出你在绘制过程中遇到的任何挑战。
- 可消费物
- 食物
- 水果
- 苹果
- 香蕉
- 蔬菜
- 青豆
- 菠菜
- 肉类
- 牛肉
- 猪肉
- 乳制品
- 奶酪
- 牛奶
- 饮料
挑战 :确定每个类别之间的准确关系。
部分类别界限模糊,例如某些食物可能同时属于多个类别,且不同分类标准会导致不同的层次结构。
4、给定一个名为Book的类,其定义包含以下字段:Author author; string title; int noOfPages; bool fiction; 为这些字段编写一组具有简单单行get和set访问器的属性。如果要为所有这些字段编写“get”和“set”方法,推荐的方法头是什么?
以下是为 Book 类字段编写的属性及“get”和“set”方法推荐的方法头:
属性:
public Author Author { get; set; }
public string Title { get; set; }
public int NoOfPages { get; set; }
public bool Fiction { get; set; }
“get”和“set”方法头:
public Author GetAuthor()
{
return author;
}
public void SetAuthor(Author author)
{
this.author = author;
}
public string GetTitle()
{
return title;
}
public void SetTitle(string title)
{
this.title = title;
}
public int GetNoOfPages()
{
return noOfPages;
}
public void SetNoOfPages(int noOfPages)
{
this.noOfPages = noOfPages;
}
public bool GetFiction()
{
return fiction;
}
public void SetFiction(bool fiction)
{
this.fiction = fiction;
}
C#核心概念与面向对象解析

最低0.47元/天 解锁文章
8758

被折叠的 条评论
为什么被折叠?



