所以我在c#中做了一个愚蠢的游戏,但我有点卡在这里.
我试图返回2个值或3个值,取决于游戏规则.
我需要知道如何只在一个函数中返回不同数量的值.就像有时我需要在一个函数中返回3个值,有时需要返回7个值.
错误tuple当然是变量,因为我不能返回少于3个值.
public Tuple<int, int, int> movepion(schaken.pion pion, int row, int column)
{
int row2 = row + 1;
//posities berekenen
if (pion.kleur == Color.Blue)
{
if (pion.volgnr == 0) { row++; row2++; }
else { row++; }
}
else
{
}
// declaration
var tuple = new Tuple<int, int>(row, column);
var tuple2 = new Tuple<int, int, int>(row, row2, column);
// what position?
if (pion.volgnr == 0)
{
pion.volgnr = pion.volgnr + 1;
return tuple2;
} …Run Code Online (Sandbox Code Playgroud) 我想在我的代码中使用goto语句去某处,但它不会跳过我的部分代码但是会继续阅读.我只需要跳过我不需要的部分......代码:
int getal1, getal2;
int som,verschil,product,quotient;
Console.WriteLine("first number: ");
Console.ForegroundColor = ConsoleColor.Green;
getal1 = Convert.ToInt32(Console.ReadLine());
Console.ResetColor();
Console.WriteLine("choose operator: -,+,*,/");
if (Console.ReadLine() == "+")
{
goto second;
}
else if (Console.ReadLine() == "-")
{
goto second1;
}
second:
Console.WriteLine("Second number(+): ");
Console.ForegroundColor = ConsoleColor.Green;
getal2 = Convert.ToInt32(Console.ReadLine());
Console.ResetColor();
goto Operat;
Operat: som = getal1 + getal2;
verschil = getal1 - getal2;
product = getal1 * getal2;
quotient = getal1 / getal2;
goto optelling;
second1: Console.WriteLine("Second number(-): ");
Console.ForegroundColor = ConsoleColor.Green;
getal2 …Run Code Online (Sandbox Code Playgroud) 我很新,我想知道如果我按下一个随机数字,但当这个数字是2时,应该有另一个文本框显示它有多少次2.
不使用if/else或switch
private void btnChoose_Click(object sender, EventArgs e)
{
double number2 = 0;
double When2 = 2;
Random number = new Random();
double chose = number.Next(1,7);
txtnumber.Text = chose.ToString();
txtnumber2.Text = something...
}
Run Code Online (Sandbox Code Playgroud)
解决方案:(数字2不可能,但数字为6)
// global var
int number6 = 0;
private void btnChoose_Click(object sender, EventArgs e)
{
double When6 = 6;
Random number = new Random();
double chose = number.Next(1,7);
txtnumber.Text = chose.ToString();
number6 = Convert.toInt32(number6 + (chose/When6));
txtnumber6.Text = number6.ToString();
}
Run Code Online (Sandbox Code Playgroud)
LB的注释: OP对其中一个删除的答案发表评论
这是一个非常烦人的任务,我只需要在没有if/ese或switch的情况下执行此操作