変数(int)
ここで必ず覚えること
Console.WriteLine()の()の中に整数を入れると、実行したときに出力される- 整数と整数を
+-*/で繋ぐと足し算・引き算・掛け算・割り算になる- この
+,-,*,/を演算子(算術演算子)というusing System;
class Program
{
static void Main(string[] args)
{
// 足し算 1 + 1 = 2
Console.WriteLine(1 + 1);
// 引き算 2 - 1 = 1
Console.WriteLine(2 - 1);
// 掛け算 2 * 2 = 4
Console.WriteLine(2 * 2);
// 割り算 6 / 2 = 3
Console.WriteLine(6 / 2);
}
} %という演算子は余り(剰余)を計算することができるusing System;
class Program
{
static void Main(string[] args)
{
// 余り(剰余)5 % 2 = 1
// 5 ÷ 2 = 2..1
Console.WriteLine(5 % 2);
}
}- 演算子の左右には半角スペース文字を空けるようにしてください
using System;
class Program
{
static void Main(string{] args)
{
// 演算子の左右に半角スペースを入れる
Console.WriteLIne(1 + 1);
}
}
- この
- 文字列は
string型であったが、整数はint型であり、変数を使うことができるint n = 10;という命令で、整数型の変数nに値10を代入しているusing System;
class Program
{
static void Main(string[] args)
{
// 変数nに10を代入(初期化処理)
int n = 10;
}
}
- 整数(int)に対してもvarを使うことができる
using System;
calss Program
{
static void Main(string[] args)
{
// 変数nに10を代入
// 右辺の値が10(整数)なので整数と判断される
var n = 10;
}
} 整数はint型だが、実数はdouble型というものが用意されている
課題
C#体験編1: C#をはじめよう
C#入門編1: プログラミングを学ぶ