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