static void Main(string[] args){
int Sum = 0;
for (int i = 1;i <= 10;i++){
if (i % 2 == 1)
Sum += i;
}
Console.WriteLine(Sum);
}
程序的输出结果:25
static void Main(string[] args){
int a=2, b=7, c=5;
switch (a > 0){
case true :
switch (b < 10){
case true: Console.Write("^"); break;
case false: Console.Write("!"); break;
}
break;
case false :
switch (c == 5){
case false: Console.Write("*"); break;
case true: Console.Write("#"); break;
}
break;
}
Console.Read();
}
程序最终的输出结果:^
static void Main(string[] args){
try{
int x = Convert.ToInt32(Console.ReadLine());
int y = Convert.ToInt32(Console.ReadLine());
int z = x / y;
}
catch (FormatException){
Console.WriteLine("格式不符");
}
catch (DivideByZeroException){
Console.WriteLine("除数不能是0");
}
catch (Exception){
Console.WriteLine("Exception!");
}
finally{
Console.WriteLine("thank you for using the program!");
}
Console.ReadLine();
}
若分别从键盘上输入5和x,则程序的最终执行结果:格式不符
thank you for using the program!
class Program{
static void Main(string[] args){
Taxi t = new Taxi();
Console.WriteLine(t.isInitialized );
Console.ReadLine();
}
}
public class Taxi{
public bool isInitialized = false;
public Taxi(){
isInitialized = true;
}
}
程序最终的输出结果:True
class Program{
static void Main(string[] args){
Mouse m = new Mouse();
m.Eat();
m.Sleep();
}
}
public abstract class Animal{
public abstract void Sleep();
public virtual void Eat(){
Console.Write ("eat something");
}
}
public class Mouse : Animal{
public override void Sleep(){
Console.Write ("mouse sleeping!");
}
public override void Eat(){
Console.Write ("eat cheese!");
}
}
程序的输出结果:eat cheese! mouse sleeping!
class Program{
static void Main(string[] args){
SharedClass sc = new SharedClass();
sc.SetDataA("interface IpartA");
Console.ReadLine();
}
}
public interface IpartA{
void SetDataA(string dataA);
}
public class SharedClass : IpartA{
private string DataA;
private string DataB;
public void SetDataA(string dataA){
DataA = dataA;
Console.WriteLine("{0}", DataA);
}
}
程序最终的输出结果是 interface IpartA
static void Main(string[] args){
int m, n, i, j, s= 0;
Console.WriteLine("请输入m,n的值");
m = Convert.ToInt32(Console.ReadLine());
n = int.Parse(Console.ReadLine());
if (m < n){
i = m;
}
else{
i = n;
}
for (j = i; j > 0; j++){
if (j % m == 0 && j % n == 0){
s = j;
break;
}
}
Console.WriteLine("{0}", s);
}
若从键盘上分别输入4和6,则程序最终的输出结果:12
static void Main(string[] args){
int[] pins = {9,3,7,2};
for (int index = 0;index != pins.Length;index++){
int pin = pins[index];
System.Console.Write(pin);
}
foreach (int i in pins){
Console.Write (i);
}
}
程序的输出结果是93729372
class A{
public void F(){
Console.Write("A");
}
public void F(string chr){
Console.Write("B");
}
}
class B:A{
public void F(){
Console.Write("B");
}
static void Main(){
B objB=new B();
objB.F();
}
}
//分析一下C#代码,根据面向对象的多态性,代码的运行结果是B
public class Person{
private int age = 0;
public int Age{
get{return age;}
set{
if(value >= 18){
Console.WriteLine("成年人");
}
else{
Console.WrteLine("未成年人");
}
}
}
}
public class People:Person{
public People(){
Console.Write("不得入内");
}
}
class Test{
static void Main (string[]args){
People shang = new People();
shang.Age = 17;
}
}
运行结果:不得入内未成年人