C# (初入江湖)-写个小猫猜数,一起玩玩吧(数组、字符串)
效果
程序分析
- 需要 PictureBox 图片控件,一张小猫图片
- 设置 Image属性,将小猫图片放入其中
- 设置 SizeMode属性 为 StretchImage 模式
-
需要4个label控件
- 1号label用于显示 提示文案:“大了!!!” 等, 默认方案为:“请输入一个1~100之间的数!!!最多猜10次哦!!!”
- 3号label用于右上角显示具体次数 0
- 4号label用于显示最后猜对了,提示之前猜过的数。开始时需要隐藏(即将Text值默认为空即可
-
需要一个TextBox框,用于输入数字
-
需要一个Button 按钮,双击自动生成点击事件,逻辑如下
- 将猜过的数放到 数组里面,最后用于显示
- 每次猜数,次数都要加1,并将其显示到界面上
- 比较输入的数与要猜的数
- 没猜对的情况给出对应的提示
- 猜对了给出提示同时显示出之前猜过的数;再将按钮置为不可点击取反 )
-
给主窗体增加一个 Load事件,窗体一启动则生成一个 1~100以内的 随机数
直接上代码
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
// 记录已猜次数
int count = 0;
// 数组,记录已经猜过的数
string[] numArray = new string[10];
// 随机生成的猜数
int guessNum = 0;
Random rd = new Random();
private void Form1_Load(object sender, EventArgs e)
{
// 窗口启动,初始化要猜的数字
guessNum= rd.Next(1, 101);
}
private void button1_Click(object sender, EventArgs e)
{
// 将输入的猜数记录到数组中, 使用猜的次数作为数组的角标
numArray[count] = this.textBox1.Text;
// 猜的次数加1
count++;
//将次数显示出来
this.label3.Text = count.ToString();
// 比较输入的数与 要猜的数
int inputNum= Convert.ToInt32(this.textBox1.Text);
if (inputNum > guessNum)
{
this.label1.Text = "大了!!!";
}
else if (inputNum < guessNum)
{
this.label1.Text = "小了!!!";
}
else
{
this.label1.Text = "恭喜你猜对了!!!";
// 将之前猜过的数字显示出来
string guessStr = "你猜过的数有:";
foreach (string str in numArray)
{
guessStr += str+" ";
}
this.label4.Text = guessStr;
//按钮不可再点击
this.button1.Enabled = false;
}
//第10次的时候,将按钮置为不可用状态
if (count == 10)
{
this.button1.Enabled = false;
this.label4.Text = "已经猜满10次了哦!!!";
}
}
}
数组与字符串
数组
- 可以将同一类型的多个变量存储在一个数组数据结构中
- 通过指定数组的元素类型来声明数组
- 如果希望数组存储任意类型的元素,可将其类型指定为 object
static void test1()
{
// 数组声明的三种方式
// 1.声明一个长度为 5 的 int类型数组
int[] array1 = new int[5];
// 2.声明一个长度为 5,同时初始化数组的内容为 1,3,5,7,9
int[] array2 = new int[] { 1, 3, 5, 7, 9 };
// 3. 2的简化版,意思与 2 相同
int[] array3 = { 1, 3, 5, 7, 9 };
//数组的操作
// 赋值的两种方式
// 1.通过角标赋值
array1[0] = 10;
array1[1] = 12;
// 2.初始化时赋值
int[] array4 = { 1, 3, 5, 7, 9 };
// 取值的两种方式
// 1.通过角标取值
Console.WriteLine(array1[0]);
Console.WriteLine(array1[1]);
// 2.通过迭代器取值
foreach(int i in array4)
{
Console.WriteLine(i);
}
// 同时将多种类型的元素
object[] objArray = { 1, "aa", 2.2, 5 };
foreach(object obj in objArray)
{
Console.WriteLine(obj);
}
}
多维数组
static void test2()
{
// 声明一个二维数组
int[,] array1 = new int[3,2];
// 声明一个三维数组
int[,,] array2 = new int[3,2,1];
// 声明一个二维数组并初始化
int[,] a2 =new int[3,2]{ { 1, 2 }, { 3, 4 }, { 5, 6 } };
// 或者这么写
int[,] a3 ={ { 1, 2 }, { 3, 4 }, { 5, 6 } };
// 赋值与取值
//赋值
array1[0,0] =2;
array1[0, 1] = 3;
//取值
Console.WriteLine(array1[0, 0]);
//循环取值
foreach(int arr in a3)
{
Console.WriteLine(arr); // output: 123456
}
}
字符串
- 字符串是值为文本的 String 类型对象
- 在 C# 中,string 关键字是 String 的别名。 因此,String 和 string 是等效的
- 字符串对象是**“不可变的”**,它们在创建后无法更改
static void test3()
{
// 字符串的声明方式
string a;
string b = null;
string c = System.String.Empty;
//字符串的转义
// 以下地址中的 \ 需要加转义字符 \, 所以变成了 \\
string oldUrl = "F:\\Program Files\\vs\\comm\\visual";
// 如果前面加上 @ ,即可避免加 \ 字符
string newUrl = @"F:\Program Files\vs\comm\visual";
// 或者改成 / 反斜杠
string newUrl2 = "F:/Program Files/vs/comm/visual";
//字符串的不可变性
string str1 = "abc";
string str2 = str1;
str1 = str1 + "edf";
Console.WriteLine(str1); //output: abcedf
Console.WriteLine(str2); //output: abc
// 字符串的常用操作
string abc = "123,444,555,aaa,BBB,ccc";
// 切割, 用逗号切割,切完后变成数组 ["123","444","555","aaa","BBB","ccc"]
string[] strArray= abc.Split(',');
Console.WriteLine( strArray[0]); // output: 123
// 大小写
string ups = "aaa";
string lps = "BBB";
Console.WriteLine(ups.ToUpper()); //ouput: AAA
Console.WriteLine(lps.ToLower()); //ouput: bbb
// 字符查找
string caZhao = "abcdefg";
Console.WriteLine(caZhao.IndexOf('c')); // output: 2 返回该字符的角标
//通过角标获取字符
Console.WriteLine(caZhao[2]); // output: c 返回该角标下的字符
//替换
string ccc = "aaa,bbb,ccc";
// 将,替换成 |
Console.WriteLine(ccc.Replace(',', '|')); // output: "aaa|bbb|ccc"
// 格式化
string ddd = "姓名:{0},年龄:{1},性别:{2}";
Console.WriteLine(string.Format(ddd, "张三", 22, "男")); //output: 姓名:张三,年龄:22,性别:男
}
更多好看的内容和好玩的案例请关注我的 WX GZH: 程序猿知秋