C#函数(EduCoder实训题目)
2021-01-08 03:31
标签:col subject temp task tput 一个 original static coder C#函数(EduCoder实训题目) 标签:col subject temp task tput 一个 original static coder 原文地址:https://www.cnblogs.com/QQ1723545340/p/13131139.html第1关:写一个函数
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace G1
{
class Program
{
static void Main(string[] args)
{
string myWords = Hello();
Console.WriteLine(myWords);
}
/********** Begin *********/
static string Hello()
{
return "Hello World, this is my function!";
}
/********** End *********/
}
}
第2关:迭代函数
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace G2
{
class Program
{
static void Main(string[] args)
{
int[] a = { 3, 4, 33, 2, 121, 34, 65, 34 };
int size = a.Length - 1;
int result = Max(a, size);
Console.WriteLine(result);
}
/********** Begin *********/
static int Max(int[] array, int i)
{
int temp;
if (i == 0)
{
return array[0];
}
else
{
temp = Max(array, i - 1);
if (temp > array[i])
{
return temp;
}
else
{
return array[i];
}
}
}
/********** End *********/
}
}
第3关:变量的作用域
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace G3
{
class Program
{
static void Main(string[] args)
{
outputDiscount();
}
/********** Begin *********/
static int originalPrice = 100;
static float discount = 0.73F;
static void outputDiscount()
{
Console.WriteLine("After discounting, the price is " + originalPrice * discount);
}
/********** End *********/
}
}
第4关:引用参数和值参数
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace G4
{
class Program
{
static void Main(string[] args)
{
int totalPrice = 100; ;
/********** Begin *********/
int originalPrice = 100;
float discount = 0.73F;
/********** End *********/
outputDiscount(originalPrice, discount, ref totalPrice);
Console.WriteLine("main totalPrice is " + totalPrice);
}
/********** Begin *********/
static void outputDiscount(int orgPrice, float dis,ref int total)
{
total = (int)(orgPrice * dis);
Console.WriteLine("After discounting, the price is " + total);
}
/********** End *********/
}
}
文章标题:C#函数(EduCoder实训题目)
文章链接:http://soscw.com/index.php/essay/40852.html