我正在做一个二十一点项目..
我首先为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 方法,因为返回应该回到它 - 我可以从按钮调用值?公共静态字符串声明有效,但前提是我有一个默认值......