gpt4 book ai didi

c# - 如何在使用 random.range 时停止获取重复数字?

转载 作者:太空宇宙 更新时间:2023-11-03 14:47:09 26 4
gpt4 key购买 nike

我正在尝试制作数学乘法应用程序。学习 Unity 和 C#。使用 Random.range 为答案按钮分配一个正确答案和 3 个随机值。但是,有时会为回答按钮分配重复的值。我的意思是相同的值被分配给两个或有时三个答案按钮。我附上了屏幕截图。谁能帮我解决这个问题??谢谢

 public class Multplication : MonoBehaviour {                            
//we make this script instance
public static Multplication instance;
//its an enum which we help use to identify the current mode of game
public enum MathsType
{
multiplication1to10,
multiplication11to20
}
//we make a variable of MathsType
public MathsType mathsType;
//2 private floats this are the question values a and b
private float a, b ;
//the variable for answer value
[HideInInspector] public float answer;
//varible whihc will assign ans to any one of the 4 answer button
private float locationOfAnswer;
//ref to the button
public GameObject[] ansButtons;
//ref to image symbol so player can know which operation is to be done
public Image mathSymbolObject;
//ref to all the symbol sprites whihc will be used in above image
public Sprite[] mathSymbols;
//get the tag of button
public string tagOfButton;
//varible to check whihc mode is this
private int currentMode;
//ref to text in scene where we will assign a and b values of question
public Text valueA , valueB;


void Awake()
{
MakeInstance();
}

//method whihc make this object instance
void MakeInstance()
{
if (instance == null)
{
instance = this;
}
}

//at start we need to do few basic setups
void Start ()
{

//we put the location value in tag of button variable
tagOfButton = locationOfAnswer.ToString();


if (GameManager.singleton != null)
{
//get whihc mode is selected
currentMode = GameManager.singleton.currentMode;
}

//we call the methods
CurrentMode();

MathsProblem();

}

//this method keeps the track of mode
void CurrentMode()
{
if (currentMode == 4)
{
//depending on the currentmode value we assign the mode

mathsType = MathsType.multiplication1to10;
}
else if (currentMode == 5)
{

mathsType = MathsType.multiplication11to20;
}


}

// Update is called once per frame
void Update ()
{
tagOfButton = locationOfAnswer.ToString();
}


//this methode calls the respective method for the respective mode
public void MathsProblem()
{

//switch case is used to assign method
switch (mathsType)
{

case (MathsType.multiplication1to10):

Multiplication1to10();

break;

case (MathsType.multiplication11to20):

Multiplication11to20();

break;
}
}

//this methode perform Multiplication process
void MultiplicationMethod()
{
b = Random.Range(0, 10);

locationOfAnswer = Random.Range(0, ansButtons.Length);

answer = a * b;

valueA.text = ("" + a).ToString();
valueB.text = ("" + b).ToString(); ;

mathSymbolObject.sprite = mathSymbols[0];

for (int i = 0; i < ansButtons.Length; i++)
{
if (i == locationOfAnswer)
{
ansButtons[i].GetComponentInChildren<Text>().text = "" + answer;
}

else
{
// the below code make sure that all the values assigned to the ans button are within the range

if (a * b <= 30)
{
ansButtons[i].GetComponentInChildren<Text>().text = "" + Random.Range( 0, 30);
}
else if (a * b <= 60 & a * b >= 31)
{
ansButtons[i].GetComponentInChildren<Text>().text = "" + Random.Range(28, 61);
}
else if (a * b <= 90 & a * b >= 61)
{
ansButtons[i].GetComponentInChildren<Text>().text = "" + Random.Range(58, 91);
}
else if (a * b <= 120 & a * b >= 91)
{
ansButtons[i].GetComponentInChildren<Text>().text = "" + Random.Range(88, 121);
}
else if (a * b <= 150 & a * b >= 121)
{
ansButtons[i].GetComponentInChildren<Text>().text = "" + Random.Range(118, 151);
}
else if (a * b <= 200 & a * b >= 151)
{
ansButtons[i].GetComponentInChildren<Text>().text = "" + Random.Range(148, 201);
}

while (ansButtons[i].GetComponentInChildren<Text>().text == "" + answer)
{
ansButtons[i].GetComponentInChildren<Text>().text = "" + Random.Range(0, 201);
}
}

}


}

void Multiplication1to10()
{
a = Random.Range(1, 10);
MultiplicationMethod();
}

void Multiplication11to20()
{
a = Random.Range(11, 20);
MultiplicationMethod();
}

最佳答案

一个解决方案的例子:我已经把简单的代码放在一起看,它看起来很统一,我保留了你的逻辑..

private int a, b;
[HideInInspector] public int answer;
//varible whihc will assign ans to any one of the 4 answer button
private int locationOfAnswer;

//this methode perform Multiplication process
void MultiplicationMethod()
{

bool reloop;
int[] keeprandom = new int[ansButtons.Length];


b = Random.Range(0, 10);

locationOfAnswer = Random.Range(0, ansButtons.Length);


answer = a * b;

valueA.text = ("" + a).ToString();
valueB.text = ("" + b).ToString(); ;

mathSymbolObject.sprite = mathSymbols[0];

for (int i = 0; i < ansButtons.Length; i++)
{
if (i == locationOfAnswer)
{
keeprandom[i] = answer;
ansButtons[i].GetComponentInChildren<Text>().text = "" + answer;
}

else
{
// the below code make sure that all the values assigned to the ans button are within the range

int value = 0;
do
{
reloop = false;
if (a * b <= 30)
{
value = Random.Range(0, 30);
}
else if (a * b <= 60 & a * b >= 31)
{
value = Random.Range(28, 61);
}
else if (a * b <= 90 & a * b >= 61)
{
value = Random.Range(58, 91);
}
else if (a * b <= 120 & a * b >= 91)
{
value = Random.Range(88, 121);
}
else if (a * b <= 150 & a * b >= 121)
{
value = Random.Range(118, 151);
}
else if (a * b <= 200 & a * b >= 151)
{
value = Random.Range(148, 201);
}

keeprandom[i] = value;
if (i == 0)
{
break;
}
for (int j = 0; j < i; j++)
{
if (keeprandom[j] == value || value == answer)
{
reloop = true;
break;
}
}

} while (reloop);

ansButtons[i].GetComponentInChildren<Text>().text = "" + value;
}

}//for loop


}

使用数组的不同逻辑的相同示例:

private int a, b;
[HideInInspector] public int answer;
//varible whihc will assign ans to any one of the 4 answer button
private int locationOfAnswer;

//this methode perform Multiplication process
void MultiplicationMethod()
{
bool reloop;
bool[]numbers = new bool[201];

b = Random.Range(0, 10);

locationOfAnswer = Random.Range(0, ansButtons.Length);


answer = a * b;
numbers[answer] = true;

valueA.text = ("" + a).ToString();
valueB.text = ("" + b).ToString(); ;

mathSymbolObject.sprite = mathSymbols[0];

for (int i = 0; i < ansButtons.Length; i++)
{
if (i == locationOfAnswer)
{
ansButtons[i].GetComponentInChildren<Text>().text = "" + answer;
}
else
{
// the below code make sure that all the values assigned to the ans button are within the range

int value = 0;
do
{
reloop = false;
if (answer <= 30)
{
value = Random.Range(0, 30);
}
else if (answer <= 60 & answer >= 31)
{
value = Random.Range(28, 61);
}
else if (answer <= 90 & answer >= 61)
{
value = Random.Range(58, 91);
}
else if (answer <= 120 & answer >= 91)
{
value = Random.Range(88, 121);
}
else if (answer <= 150 & answer >= 121)
{
value = Random.Range(118, 151);
}
else if (answer <= 200 & answer >= 151)
{
value = Random.Range(148, 201);
}


if (numbers[value]) //already select?
{
reloop = true;
}

} while (reloop);

numbers[value] = true;
ansButtons[i].GetComponentInChildren<Text>().text = "" + value;
}

}//for loop
}

关于c# - 如何在使用 random.range 时停止获取重复数字?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53672723/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com