Windows Phone开发的书籍,正如iOS开发一样,是那么的少,苹果有斯坦福的公开课,而WP7也有她的白胡子老头的<<WP7 四天速成教程>>,在看了他的教程他所谓的四天中的不到两天之后,我写了这个简单的<<猜数字>>应用,这个程序就是在折腾着字符串,应用地址(http://windowsphone.com/s?appid=e8cc529e-1672-494a-b8a3-8c11aa531e59)
创建一个最简单程序,它就好像是HELLO WORLD一样,随意没事儿的时候,可以自己写着玩,我们拖控件,之后写事件,声明4个变量用于得到4个随机数字,
第一段代码,用于按键"1"的事件:
private void button1_Click(object sender, RoutedEventArgs e)
{
if(textBlockNow.Text.Length<4){
int tmp = 0;
for (int i = 0; i < textBlockNow.Text.Length; i++) {
if (textBlockNow.Text.ElementAt(i).ToString().CompareTo("1") == 0) {
tmp++;
}
} if (tmp == 0)
{
textBlockNow.Text = textBlockNow.Text + "1";
}
}
}
if (textBlockNow.Text.Length == 4) {
if (textBlockshow1.Text.Length == 0)
{
textBlockshow1.Text = textBlockNow.Text;
textBlockNow.Text = "";
if ((test(textBlockshow1.Text.ToString())).CompareTo("4A0B") == 0)
{
textBlockshow2.Text = "WINNER";
textBlockshow3.Text = "WINNER";
textBlockshow4.Text = "WINNER";
textBlockshow5.Text = "WINNER";
textBlockshow6.Text = "WINNER";
textBlockshow7.Text = "WINNER";
textBlockshow8.Text = "WINNER";
textBlockwin.Text = "YOU ARE WINNER!!!!!";
}
textBlockshow1.Text = textBlockshow1.Text + "......" + test(textBlockshow1.Text.ToString());
}
else if (textBlockshow2.Text.Length == 0)
{
textBlockshow2.Text = textBlockNow.Text;
textBlockNow.Text = "";
if ((test(textBlockshow2.Text.ToString())).CompareTo("4A0B") == 0)
{
textBlockshow3.Text = "WINNER";
textBlockshow4.Text = "WINNER";
textBlockshow5.Text = "WINNER";
textBlockshow6.Text = "WINNER";
textBlockshow7.Text = "WINNER";
textBlockshow8.Text = "WINNER";
textBlockwin.Text = "YOU ARE WINNER!!!!!";
}
textBlockshow2.Text = textBlockshow2.Text + "......" + test(textBlockshow2.Text.ToString());
}
......
public string test(string str) {
int A = 0;
int B = 0;
int tempnum = int.Parse(str);
int tempn1 = tempnum / 1000;
int tempn2 = tempnum % 1000 /100;
int tempn3 = tempnum % 1000 % 100 / 10;
int tempn4 = tempnum % 10;
if (tempn1 == num1) { A++; } else if (tempn1 == num2 || tempn1 == num3 || tempn1 == num4) { B++; }
if (tempn2 == num2) { A++; } else if (tempn2 == num1 || tempn2 == num3 || tempn2 == num4) { B++; }
if (tempn3 == num3) { A++; } else if (tempn3 == num1 || tempn3 == num2 || tempn3 == num4) { B++; }
if (tempn4 == num4) { A++; } else if (tempn4 == num1 || tempn4 == num3 || tempn4 == num2) { B++; }
string string02 = A.ToString() + "A" + B.ToString() + "B";
return string02;
}
适合初学者,练习对字符串的操作