C#练习题--第二篇

class Program{ 
	static void Main(string[] args){             
		MyClass m = new MyClass();             
		int a, b, c;             
		a = 0; 
		b =20; 
		c = 10;             
		m.sort(ref a, ref b, ref c); 
		Console.WriteLine("a={0},b={1},c={2}",a,b,c);            
		Console.ReadLine();         
	}                    
} 

class MyClass{         
	public void sort(ref int x, ref int y, ref int t){             
		int tmp;             
		if (x > y) { tmp = x; x = y; y = tmp; }             
		if (x > t) { tmp = x; x = t; t = tmp; }             
		if (y > t) { tmp = y; y = t; t = tmp; }             
		Console.WriteLine("{0},{1},{2}", x, y, t);         
	}       
}

程序的输出结果:a=0,b=10,c=20
class Program{ 
	static void Main(string[] args){             
		MyClass m = new MyClass();             
		int[] s ={ 34, 23, 65, 67, 54, 98, 6, 56 };             
		m.Array(s);             
		for(int i=0;i<s.Length;i++){                 
			Console .Write("{0}",s [i]);             
		}             
		Console .ReadLine ();         
	}     
} 

class MyClass{        
	public void Array(int[] a){             
		for (int i = 0; i < a.Length; i++){                 
			a[i] = i;              
		}         
	}   
} 

程序最终的输出结果:01234567
class Program{
	enum Season:int{Spring,Summer,Autumn,Winter} 
	static void Main(string[] args){                
		Season s = (Season)2;                
		Console.WriteLine(s);             
		Console.Read();         
	}     
} 

程序的输出结果:Autumn
class Program{         
	static void Main(string[] args){            
		int i = 0, sum = 0;             
		do{                 
			sum++;             
		}             
		while (i > 0);             
		Console.WriteLine("sum = {0}", sum);         
	}     
}
 
程序的运行结果:sum = 1
static void Main(string[] args){ 
	int x = 123; 
	object obj1=x;   
	x = x+100;                   
	Console.WriteLine (" obj1= {0}" , obj1 );             
} 

程序的输出结果:obj1=123
下面程序的功能是:输出100以内能被3整除且个位数为6的所有整数
class Program{ 
    static void Main(string[] args){
            int j;
            for (int i = 0; i <= 9; i++){
                j = i * 10 + 6;
                if (j % 3 != 0){
                    continue;
                }
                Console.Write("{0} ", j);
            }
            Console.Read();    
    }    
        
}

运行结果:6 36 66 96
class Program{    
    static void Main(string[] args){
		int m, n, i, j, max = 0;
        Console.WriteLine("请输入m,n的值");
        Regex regex = new Regex(@"(\d+)\s+(\d+)");
        Match match = regex.Match(Console.ReadLine());
        if (match.Success){
            int.TryParse(match.Groups[1].Value, out m);
            int.TryParse(match.Groups[2].Value, out n);
            if (m < n)
                i = m;
            else
                i = n;
            for (j = i; j > 0; j--)
                if (m % j == 0 && n % j == 0){
                    max = j;
                    break;
                }
            Console.WriteLine("max={0}", max);
            Console.ReadLine();
        }
            
    }    
        
}

若分别从键盘输入8和6,则程序的运行结果:max=2
static void Main(string[] args){         
	int Sum = 0;                          
	for (int i = 1;i <= 10;i++){              
		if (i % 2 == 0)                                        
			Sum += i;        
	} 
	Console.WriteLine(Sum);     
} 

程序的输出结果:30
static void Main(string[] args){              
	int max, min, i;          
	int[] a = new int[6]{14,4,6,8,10,12};             
	max = min = a[0];          
	for (i = 1; i < 6;i++){                  
		if (a[i] > max) 
			max = a[i];                 
		if (a[i] < min) 
			min = a[i];             
	}              
	Console.WriteLine("{0}, {1}",max, min);             
	Console.ReadLine();         
}

程序最终的输出结果:14, 4
class Program{
    static void Main(string[] args){
        for (int i = 1; i <= 10; i++){
            Console.Write(i);
            if (i % 5 == 0)
                Console.WriteLine();
            else
                Console.Write('\t');
        }    
    }            
}

 程序的运行结果:    1    2    3    4    5 
			      6    7    8    9    10

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值