開発環境
- OS: Windows 10 Pro
- IDE(統合開発環境): Visual Studio Community 2017
Head First C# ―頭とからだで覚えるC#の基本 (Andrew Stellman (著)、Jennifer Greene (著)、佐藤 嘉一 (監修, 監修)、木下 哲也 (翻訳)、オライリージャパン)の3章(オブジェクト指向になる! - わかりやすいコードにする)、プールパズル(p. 109)を取り組んでみる。
コード
MainPage.xaml
<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:local="clr-namespace:App1"
x:Class="App1.MainPage">
<StackLayout>
<!-- Place new controls here -->
<!--
<Label Text="Welcome to Xamarin.Forms!"
HorizontalOptions="Center"
VerticalOptions="CenterAndExpand" />
-->
<Button x:Name="button1"
Text="10回"
Clicked="button1_Clicked"/>
<Button x:Name="button2"
Text="24回"
Clicked="button2_Clicked" />
</StackLayout>
</ContentPage>
MainPage.xaml.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Xamarin.Forms;
namespace App1
{
public partial class MainPage : ContentPage
{
public MainPage()
{
InitializeComponent();
}
private void button1_Clicked(object sender, EventArgs e)
{
string result = "";
Echo echo1 = new Echo();
Echo echo2 = new Echo();
int x = 0;
while (x < 4)
{
result += echo1.hello() + "\n";
echo1.count += 1;
if (x > 0)
{
echo2.count += 1;
}
if (x > 1)
{
echo2.count += echo1.count;
}
x += 1;
}
DisplayAlert("", result + "回数: " + echo2.count, "OK");
}
private void button2_Clicked(object sender, EventArgs e)
{
string result = "";
Echo echo1 = new Echo();
Echo echo2 = echo1;
int x = 0;
while (x < 4)
{
result += echo1.hello() + "\n";
echo1.count += 1;
if (x > 0)
{
echo2.count += 1;
}
if (x > 1)
{
echo2.count += echo1.count;
}
x += 1;
}
DisplayAlert("", result + "回数: " + echo2.count, "OK");
}
}
public class Echo
{
public int count = 0;
public string hello()
{
return "こんにちわぁぁぁぁ・・・";
}
}
}
0 コメント:
コメントを投稿