猜数字游戏

2025-01-19 08:24:11
推荐回答(3个)
回答1:

using System;

using System.Collections.Generic;

using System.ComponentModel;

using System.Data;

using System.Drawing;

using System.Linq;

using System.Text;

using System.Windows.Forms;

namespace ccc

{

    public partial class Form1 : Form

    {

        public Form1()

        {

            InitializeComponent();

        }

        int sum = 0;

        private void button2_Click(object sender, EventArgs e)

        {

            sum = 0;

            Random ran = new Random();

            string[] strs = new string[4];

            string num1 = ran.Next(1, 9).ToString();

            strs[0] = num1;

        nu1:

            string num2 = ran.Next(0, 9).ToString();

            if (num2 == num1)

            {

                goto nu1;

            }

            strs[1] = num2;

        nu2:

            string num3 = ran.Next(0, 9).ToString();

            if (num3 == num1 || num3 == num2)

            {

                goto nu2;

            }

            strs[2] = num3;

        nu3:

            string num4 = ran.Next(0, 9).ToString();

            if (num4 == num1 || num4 == num2 || num4 == num3)

            {

                goto nu3;

            }

            strs[3] = num4;

            this.label1.Text = num1 + num2 + num3 + num4;

            this.label4.Text = "随机数已生成";

        }

        private void button1_Click(object sender, EventArgs e)

        {

            if (this.maskedTextBox1.Text == "")

            {

                MessageBox.Show("请输入您猜的数!");

            }

            else if (this.maskedTextBox1.Text.Length != 4)

            {

                MessageBox.Show("请输入完整!");

            }

            else if (this.maskedTextBox1.Text == this.label1.Text)

            {

                if (sum == 0)

                {

                    this.label2.Text = string.Format("您真棒!猜了一次就猜对了!\r最终得分是100分!O(∩_∩)O哈哈");

                }

                else

                {

                    this.label2.Text = string.Format("恭喜您!您一共猜了{0}次,\r,最终得分是{1}分!", sum+1, 100 - sum);

                }

            }

            else

            {

                sum++;

                //判断位置输入正确的数字个数

                int weizhi = 0;

                for (int i = 0; i < this.maskedTextBox1.Text.Length; i++)

                {

                    if (this.maskedTextBox1.Text[i] == this.label1.Text[i])

                    {

                        weizhi++;

                    }

                }

                //判断数字正确单位置不正确的数字个数

                int shuzi = 0;

                for (int i = 0; i < this.maskedTextBox1.Text.Length; i++)

                {

                    if (this.maskedTextBox1.Text[i] != this.label1.Text[i])

                    {

                        for (int j = 0; j < this.label1.Text.Length; j++)

                        {

                            if (this.maskedTextBox1.Text[i] == label1.Text[j])

                            {

                                shuzi++;

                                break;

                            }

                            else

                            {

                                if (j < 4)

                                {

                                    continue;

                                }

                            }

                        }

                    }

                }

                this.label2.Text = string.Format("您位置输入正确的数字个数为{0}个\r数字正确位置不正确的数字个数为{1}个!", weizhi, shuzi);

            }

        }

    }

}

回答2:

#include
#include
#include
using namespace std;

int main()
{
time_t t = time(0);
cout << ctime(&t)< char flag = 'y';
while (flag == 'y')
{
cout << "请输入4个不一样的数字,若位置正确表示为A,位置不正确表示为B,10次都猜不出来为输" << endl;
srand(GetTickCount());
int i, j;
char* sour = new char[4];
for (i = 0; i != 4; ++i)
{
sour[i] = rand() % 10 + 48;
for (j = 0; j < i; ++j)
{
if (sour[i] == sour[j])
{
i--;
}
}
}
char* gues = new char[4];
unsigned int a = 0;
unsigned int b = 0;
unsigned int time = 1;
while(a != 4 && time <= 10)
{
a = 0;
b = 0;
cout << time << ".请输入你猜的数字:" << "\t";
cin >> gues;
for (i = 0; i != 4; ++i)
{
if (gues[i] == sour[i])
{
a++;
}
}
for (i = 0; i != 4; ++i)
{
for (j = 0; j != 4; ++j)
{
if (i != j && gues[i] == sour[j])
{
b++;
}
}
}
cout << "\t\t\t" << a << "A" << b << "B" << endl;
time++;
}
if (time == 11)
{
cout << "很遗憾,你输了!" << endl;
cout << "正确答案是:" << sour << endl;
}
else
{
cout << "恭喜你!猜对了!" << endl;
}
cout << "是否继续?(y/n)";
cin >> flag;
cout << endl;
}

// system("pause");
return 0;
}

回答3:

以前写过一个,当然,没做界面的,要做界面自己可以做
#include
#include
#include
using namespace std;

int main()
{
time_t t = time(0);
cout << ctime(&t)< char flag = 'y';
while (flag == 'y')
{
cout << "请输入4个不一样的数字,若位置正确表示为A,位置不正确表示为B,10次都猜不出来为输" << endl;
srand(GetTickCount());
int i, j;
char* sour = new char[4];
for (i = 0; i != 4; ++i)
{
sour[i] = rand() % 10 + 48;
for (j = 0; j < i; ++j)
{
if (sour[i] == sour[j])
{
i--;
}
}
}
char* gues = new char[4];
unsigned int a = 0;
unsigned int b = 0;
unsigned int time = 1;
while(a != 4 && time <= 10)
{
a = 0;
b = 0;
cout << time << ".请输入你猜的数字:" << "\t";
cin >> gues;
for (i = 0; i != 4; ++i)
{
if (gues[i] == sour[i])
{
a++;
}
}
for (i = 0; i != 4; ++i)
{
for (j = 0; j != 4; ++j)
{
if (i != j && gues[i] == sour[j])
{
b++;
}
}
}
cout << "\t\t\t" << a << "A" << b << "B" << endl;
time++;
}
if (time == 11)
{
cout << "很遗憾,你输了!" << endl;
cout << "正确答案是:" << sour << endl;
}
else
{
cout << "恭喜你!猜对了!" << endl;
}
cout << "是否继续?(y/n)";
cin >> flag;
cout << endl;
}

// system("pause");
return 0;
}