開発環境
- Microsoft Windows 8 Pro 64bit 日本語 (OS)
- Microsoft Visual Studio Express 2012 for Windows Desktop (IDE)
- プログラミング言語: C#
『初めてのC# 第2版』(Jesse Liberty+Brian MacDonald著、日向俊二訳、オライリー・ジャパン、2006年、ISBN978-487312-194-7)の 第8章(メソッド)8.5(練習問題)練習8-3.を解いてみる。
その他参考書籍
練習8-3.
コード
using System; class Tester { public void DoublerAndTripler(int a, out int b, out int c) { b = 2 * a; c = 3 * a; } public void Run() { int a = 10; int b, c; DoublerAndTripler(a, out b, out c); Console.WriteLine("2 * {0} = {1}", a, b); Console.WriteLine("3 * {0} = {1}", a, c); } static void Main() { Tester t = new Tester(); t.Run(); } }
入出力結果(Console Window)
2 * 10 = 20 3 * 10 = 30 続行するには何かキーを押してください . . .
ちなみにJavaScriptの場合。
コード(BBEdit)
// とりあえずthisを使ってグローバル変数を定義してみる事に function doublerAndTripler( a ) { this.b = 2 * a; this.c = 3 * a; } var a = 10; doublerAndTripler(a); $('#pre0').text( a + " * 2 = " + b + "\n" + a + " * 2 = " + c);
pythonの場合。
コード(BBEdit)
sample.py
#!/usr/bin/env python3.3 #-*- coding: utf-8 -*- # JavaScriptのthisみたいにglobalを使ってみたけど、C#の場合とはちょっと意味が違ってくるかな。。 def doublerAndTripler(a): global b global c b = 2 * a c = 3 * a a = 10 doublerAndTripler(a) print("2 * {0} = {1}\n3 * {0} = {2}".format(a, b, c))
入出力結果(Terminal)
$ ./sample.py 2 * 10 = 20 3 * 10 = 30 $
0 コメント:
コメントを投稿