using System;
class SampleClass1
{
public int sum(params int[] numArray)
{
int num=0;
foreach(var n in numArray)
{
num+=n;
}
return num;
}
}
class SampleClass2
{
public int time(params int[] numArray)
{
int num=1;
foreach(var n in numArray)
{
num*=n;
}
return num;
}
}
class MainClass
{
static void Main()
{
// SampleClass1,2をインスタンス化
SampleClass1 sample1 = new SampleClass1();
SampleClass2 sample2 = new SampleClass2();
// 配列を定義
var numArray1 = new int[10];
var numArray2 = new int[100];
// 配列の要素に値を代入
for (int i = 0; i < 10; i++)
{
numArray1[i] = ++i;
i--;
}
for (int i = 0; i < 100; i++)
{
numArray2[i] = ++i;
i--;
}
Console.WriteLine("1+2+・・・+10={0}",
sample1.sum(numArray1));
Console.WriteLine("1+2+・・・+100={0}",
sample1.sum(numArray2));
Console.WriteLine("1*2*・・・*10={0}",
sample2.time(numArray1));
Console.WriteLine("1*3*5*7*10={0}",
sample2.time(1,3,5,7,10));
}
}
0 コメント:
コメントを投稿