C#を使うようにはなったものの、具体的な機能については未習熟なので(というかまだ使うべき状況が出てきていないので)
を読んで、まずは最低ラインを学んでいく。
5. 繰り返し処理
5.1 for文
{}で囲まれた処理を指定した条件が満たされている間、繰り返す処理をfor文という。
for (初期化処理 ; 条件式 ; 増分処理)
{
処理
}
増分処理として「インクリメント」(変数の値を増やす処理)または「デクリメント」(変数の値を
減らす処理)が頻用される。
呼び名 |
意味 |
|
| i++; | インクリメント(後置) | 変数の値を |
| ++i; | インクリメント(前置) | 変数の値を |
| i--; | デクリメント(後置) | 変数の値を |
| --i; | デクリメント(前置) | 変数の値を |
前置は演算の終了後に左辺の値に代入される。後置は代入の後に演算を行う。
int a1=1,b1=1,c1=1,d1=1; int a2,b2,c2,d2; a2=a1++; //a1は2, a2は1になる b2=++b1; //b1は2, b2は2になる c2=c1--; //c1は0, c2は1になる d2=--d1; //d1は0, d2は0になる
for文をネストしたものを多重ループという。高々二重程度が限度である。
5.2 while文
()内の条件が成り立つ間、{}内に記述されている処理を繰り返すのがwhile文である。
while(条件式)
{
処理
}
またdo~while文もある:
do
{
処理
}
while(条件式);
条件式の適用の順番が相違し、do~while文ではまず処理がなされた後に条件式が適用される。したがって少なくとも1回処理が実行される。
5.3 例題8
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ExampleProblem301
{
public class Program
{
static void Main(string[] args)
{
Console.Write("★の数を入力して下さい:");
int n = int.Parse(Console.ReadLine());
int i = 0;
if (n < 0)
{
}
else
{
while (i < n)
{
Console.Write("★");
i++;
}
Console.WriteLine();
}
}
}
}
5.4 例題9
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ExampleProblem302
{
public class Program
{
static void Main(string[] args)
{
Console.Write("正の整数を入力:");
int n = int.Parse(Console.ReadLine());
if (n >= 2)
{
int i = 1;
while (i < n)
{
if (n % i == 0)
{
Console.Write("{0}", i);
}
i++;
}
Console.WriteLine(" ");
}
}
}
}
5.5 例題10
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ExampleProblem303
{
public class Program
{
static void Main(string[] args)
{
Console.Write("1から10までの整数を入力して下さい。");
Random rnd = new Random();
int n = rnd.Next(11);
if (1<=n&&n<=10)
{
Console.Write("1回目:");
int k = int.Parse(Console.ReadLine());
int i = 1;
while(i <= 3)
{
Console.Write("{0}回目:", i);
int ans = int.Parse(Console.ReadLine());
if(ans == n)
{
Console.WriteLine("正解!");
break;
}
else if(ans < n)
{
Console.WriteLine("小さすぎます。");
}
else
{
Console.WriteLine("大きすぎます。");
}
i++;
}
if (i == 4)
{
Console.WriteLine("ゲームオーバー!");
Console.WriteLine("正解は{0}です", n);
}
}
}
}
}
5.6 例題11
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ExampleProblem304
{
public class Program
{
static void Main(string[] args)
{
var variables = new List<int>();
var rnd = new Random();
int min = 101;
int max = 0;
for (int i = 0; i < 10; i++)
{
variables.Add(rnd.Next(1,101));
max = Math.Max(max, variables[i]);
min = Math.Min(min, variables[i]);
}
Console.WriteLine("最大値:{0}", max);
Console.WriteLine("最小値:{0}", min);
}
}
}
5.7 例題12
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ExampleProblem305
{
public class Program
{
static void Main(string[] args)
{
Console.WriteLine("九九の一覧表を出力:");
for(int row = 1; row < 9; row ++)
{
for(int col = 1; col < 9; col++)
{
Console.Write("{0}×{1}={2,2:d}",row,col,row*col);
}
Console.WriteLine();
}
}
}
}
なお桁揃えの書式は以下の通り:
希望する出力書式例 |
出力例 |
書式 |
| 10進法 | 100 | {0:d} |
| 10進法8桁左詰 | 100_____ | {0,-8:d} |
| 10進法8桁右詰 | _____100 | {0,k:d} |
| 10進法6桁0埋 | 000100 | {0:d6} |
| 10進法8桁0埋 左詰6桁 | 000100__ | {0-8:d6} |
| 10進法8桁0埋 右詰6桁 | __000100 | {0,8:d6} |
6. 配列
同一の名前で多数のデータを格納できる変数を配列(変数)という。配列に格納される個々のデータを要素という。
配列は以下で宣言する:
- 1次元配列の場合
(変数の型名)[] 変数名 = new 変数の型名[配列の数]
- 2次元配列の場合
(変数の型名)[,] 変数名 = new 変数の型名[配列の数,配列の数]
- 多次元配列の場合
(変数の型名)[,,\cdots,] 変数名 = new 変数の型名[配列の数,配列の数,\cdots,配列の数]
配列の要素を指定する際、C#はその順番をから始める。
6.1 ジャグ配列
配列はその要素として配列を格納させることが可能であり、その結果として構造を不揃いなものにしても問題ない。
6.3 foreachによる繰り返し
配列を対象とした繰り返し処理としてforeach文がある。
foreach(変数 in 配列)
{
処理
}
6.4 例題13
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ExampleProblem306
{
public class Program
{
static void Main(string[] args)
{
// 変数の宣言
var ret = new int[5];
int max = 0;
int min = 0;
// 入力値の格納
for(int i = 0; i < ret.Length; i++)
{
Console.Write("{0}つ目の数値", i + 1);
ret[i] = int.Parse(Console.ReadLine());
}
// 入力値の出力、最大・最小の取得
for(int i = 0; i < ret.Length; i++)
{
Console.Write("{0}", ret[i]);
if (i == 0)
{
max = ret[i];
min = ret[i];
}
else
{
if (ret[i] > max)
{
max = ret[i];
}
if (ret[i] < min)
{
min = ret[i];
}
}
}
// 最大・最小の出力
Console.WriteLine("最大値:{0}", max);
Console.WriteLine("最小値:{0}", min);
}
}
}
