0

我正在做一个二十一点项目..

我首先为Button创建了Hit类。Hit

因此,在表单上,​​当我单击它时,它将从 switch case 场景中检索一个值,该值在命中类中随机化了一个数字/值。

我尝试在方法中的 Class hit to = stRefID 下创建一个变量,GenerateID但这也失败了;一切都是公开的,所以我很困惑。

感谢您抽出宝贵时间帮助我,因为这是我第一次在这里发帖,但我一直都在使用这个地方。

命中.cs:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Blackjack
{
public class Hit
{
    public static string GenerateID(int MinSize, int MaxSize)
    {

        string stRefID = "";

        Random random = new Random();

            int iChosenMaxSize = random.Next(0, 11);

            int two = 2;
            int three = 3;
            int four = 4;
            int five = 5;
            int six = 6;
            int seven = 7;
            int eight = 8;
            int nine = 9;
            int ten = 10;
            int jack = 10;
            int queen = 10;
            int king = 10;
            int ace = 11;


         for (int x = 1; x <= iChosenMaxSize; x++)
        {
            int iCharType = random.Next(0, 12);
            switch (iCharType)
            {
                    case 0:
                        stRefID += two;
                        break;
                    case 1:
                        stRefID += three;
                        break;
                    case 2:
                        stRefID += four;
                        break;
                    case 3:
                        stRefID += five;
                        break;
                    case 4:
                        stRefID += six;
                        break;
                    case 5:
                        stRefID += seven;
                        break;
                    case 6:
                        stRefID += eight;
                        break;
                    case 7:
                        stRefID += nine;
                        break;
                    case 8:
                        stRefID += ten;
                        break;
                    case 9:
                        stRefID += ace;
                        break;
                    case 10:
                        stRefID += jack;
                        break;
                    case 11:
                        stRefID += queen;
                        break;
                    case 12:
                        stRefID += king;
                        break;

                }
             } return stRefID; 

        }
    }
}

我想在哪里检索并将文本框设置为检索到的值:

using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Blackjack
{
public partial class Form1 : Form
{


        private void hit_Click(object sender, EventArgs e)
    {

         this.test.Text = Hit.stRefID;

    }

我也尝试过公开按钮,但这失败了......

输出测试文本框:

private void test_TextChanged(object sender, EventArgs e)
    {

    }

亲切的问候,迈克。

修改后的代码:

namespace Blackjack
{
public class Hit
{

    public static string GenerateID(string stRefID)

    {

        Random random = new Random();

            int iChosenMaxSize = random.Next(0, 11);

            int two = 2;
            int three = 3;
            int four = 4;
            int five = 5;
            int six = 6;
            int seven = 7;
            int eight = 8;
            int nine = 9;
            int ten = 10;
            int jack = 10;
            int queen = 10;
            int king = 10;
            int ace = 11;


         for (int x = 1; x <= iChosenMaxSize; x++)
        {
            int iCharType = random.Next(0, 12);
            switch (iCharType)
            {
                    case 0:
                        stRefID += two;
                        break;
                    case 1:
                        stRefID += three;
                        break;
                    case 2:
                        stRefID += four;
                        break;
                    case 3:
                        stRefID += five;
                        break;
                    case 4:
                        stRefID += six;
                        break;
                    case 5:
                        stRefID += seven;
                        break;
                    case 6:
                        stRefID += eight;
                        break;
                    case 7:
                        stRefID += nine;
                        break;
                    case 8:
                        stRefID += ten;
                        break;
                    case 9:
                        stRefID += ace;
                        break;
                    case 10:
                        stRefID += jack;
                        break;
                    case 11:
                        stRefID += queen;
                        break;
                    case 12:
                        stRefID += king;
                        break;

                }
          } return stRefID; 

        }

}
}

我用 stRefID 制作了 generateID 方法,因为返回应该回到它 - 我可以从按钮调用值?公共静态字符串声明有效,但前提是我有一个默认值......

4

3 回答 3

4

不确定我是否完全理解。

但我可以看到这stRefID是一个local可以GenerateID发挥作用的变量。

如果您需要访问它,Hit.stRefID那么stRefID应该是静态类成员

尝试添加stRefIDstatic全局Class变量。

public class Hit
{
    public static string stRefID = "";

    public static string GenerateID(int MinSize, int MaxSize)
    {
       //Your logic
    }
于 2013-05-20T03:50:30.120 回答
2

您不能访问在方法内定义的变量。stRefID 的范围严格在 GenerateID 之内。根据你的类,如果你想访问 stRefID 的值,你需要调用Hit.GenerateID,这将返回 stRefID 的值。变量的范围在 C# 中非常重要(与所有编程语言一样)。我建议你看看C# Variable Scope

于 2013-05-20T03:50:50.260 回答
-1

你所做的就是stRefID在方法的范围内声明变量,GenerateID这是行不通的,所以你可以做的是将该变量放在类的范围内,然后你可以像这样使用它。

namespace Blackjack
   {
    public class Hit
      {
       string stRefID = "";

       public static string GenerateID(int MinSize, int MaxSize)
        {
        Random random = new Random();

        int iChosenMaxSize = random.Next(0, 11);

        int two = 2;
        int three = 3;
        int four = 4;
        int five = 5;
        int six = 6;
        int seven = 7;
        int eight = 8;
        int nine = 9;
        int ten = 10;
        int jack = 10;
        int queen = 10;
        int king = 10;
        int ace = 11;


     for (int x = 1; x <= iChosenMaxSize; x++)
    {
        int iCharType = random.Next(0, 12);
        switch (iCharType)
        {
                case 0:
                    stRefID += two;
                    break;
                case 1:
                    stRefID += three;
                    break;
                case 2:
                    stRefID += four;
                    break;
                case 3:
                    stRefID += five;
                    break;
                case 4:
                    stRefID += six;
                    break;
                case 5:
                    stRefID += seven;
                    break;
                case 6:
                    stRefID += eight;
                    break;
                case 7:
                    stRefID += nine;
                    break;
                case 8:
                    stRefID += ten;
                    break;
                case 9:
                    stRefID += ace;
                    break;
                case 10:
                    stRefID += jack;
                    break;
                case 11:
                    stRefID += queen;
                    break;
                case 12:
                    stRefID += king;
                    break;

            }
         } return stRefID; 

    }

我想这应该可以正常工作

于 2013-05-20T03:55:39.643 回答