我正在制作二十一点项目..
我首先为Hit
按钮创建了Hit
类。
因此,在表单上,当我单击它时,它将从switch案例场景中检索一个值,该值已在命中类中随机化了一个数字/值。
我尝试在GenerateID
方法中的Class hit to = stRefID下创建一个变量,但这也失败了;一切都是公开的,所以我很困惑。
感谢您的时间和帮我这个,因为这是我第一次在这里发帖,但我一直都在这个地方。
Hit.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方法,因为返回应该返回到它 - 我可以从按钮调用值?公共静态字符串声明有效但只有我有一个默认值...
答案 0 :(得分:4)
不确定我是否完全理解。
但我可以看到stRefID
是变量local
到GenerateID
函数。
如果您需要以Hit.stRefID
方式访问它,那么stRefID
应该是一个静态类成员
尝试将stRefID
添加为static
全局Class
变量。
public class Hit
{
public static string stRefID = "";
public static string GenerateID(int MinSize, int MaxSize)
{
//Your logic
}
答案 1 :(得分:2)
您无法访问方法内定义的变量。 stRefID的范围严格在GenerateID中。根据您的类,如果要访问stRefID的值,则需要调用Hit.GenerateID
,这将返回stRefID的值。变量的范围在C#中非常重要(与所有编程语言一样)。我建议你看看C# Variable Scope
答案 2 :(得分:-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;
}
这应该可以正常工作我猜