プログラミング(Python、Perl、C、Go、JavaScript)、数学、読書…
開発環境
"初めてのC# 第2版"の第8章(メソッド)の8.5(練習問題)を解いてみる。
練習 8-1
using System; class Tester { public int Doubler(int n) { return n * 2; } public float Doubler(float n) { return n * 2.0F; } public void Run() { int n = 1; float f = 1.2F; Console.WriteLine( "n={0} f={1}\nn*2={2}\nm*2={3}", n, f, Doubler(n), Doubler(f)); } static void Main() { Tester t = new Tester(); t.Run(); } }
出力結果
n=1 f=1.2 n*2=2 m*2=2.4 続行するには何かキーを押してください . . .
練習 8-2
using System; class Tester { public void DoubleAndTriple(int n, ref int m, ref int l) { m = n * 2; l = n * 3; } public void Run() { int n = 1; int m = 0; int l = 0; DoubleAndTriple(n, ref m, ref l); Console.WriteLine( "n={0}\nn*2={1}\nn*3={2}", n, m, l); } static void Main() { Tester t = new Tester(); t.Run(); } }
n=1 n*2=2 n*3=3 続行するには何かキーを押してください . . .
練習 8-3
using System; class Tester { public void DoubleAndTriple(int n, out int m, out int l) { m = n * 2; l = n * 3; } public void Run() { int n = 1; int m; int l; DoubleAndTriple(n, out m, out l); Console.WriteLine( "n={0}\nn*2={1}\nn*3={2}", n, m, l); } static void Main() { Tester t = new Tester(); t.Run(); } }
0 コメント:
コメントを投稿