如何将此代码(VB)转换为C#? [英] How can I convert this code(VB) to C# ?

查看:97
本文介绍了如何将此代码(VB)转换为C#?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请帮助我!
如何将VB代码转换为C#?

Please help me!
How can I convert this VB code to C# ?

Dim i, j, Rand As Integer
        ClicksMouse = 0
        For i = 1 To 6
            Radombutton(i).Image = Nothing
        Next

        For i = 1 To 6
A1:
            Randomize()
            Rand = Int(6 * Rnd() + 1)

            'Kiểm tra trùng này !
            For j = 1 To i - 1
                If Rand = List1(j) Then GoTo A1
            Next
            List1(i) = Rand
        Next

推荐答案

尝试:
int i = 0;
int j = 0;
int Rand = 0;
ClicksMouse = 0;
for (i = 1; i <= 6; i++) {
	Radombutton(i).Image = null;
}
for (i = 1; i <= 6; i++) {
	A1:
	VBMath.Randomize();
	Rand = Conversion.Int(6 * VBMath.Rnd() + 1);
	//Kiểm tra trùng này !
	for (j = 1; j <= i - 1; j++) {
		if (Rand == List1(j))
			goto A1;
	}
	List1(i) = Rand;
}

未经测试:我使用C#的网站< => VB转换: http://www.developerfusion.com/tools/convert/vb-to-csharp/ [^ ]

This is not tested: I use a website for C# <=> VB conversion: http://www.developerfusion.com/tools/convert/vb-to-csharp/[^]




这是手动翻译的,但仍未经测试(而且我不知道您要如何操作..

无论如何,这是部分重构的代码:

Hi,

This is manually translated but still untested (and I don''t know what you''re trying to do with this..

Anyway, here is the partially-refactored code:

public void Method()
{
    Random rnd = new Random();
    for (int i = 1; i <= 6; i++)
    {
        // RandomButton is a collection, right?
        RandomButton[i].Image = null;
    }
    for (int i = 1; i <= 6; i++)
    {
        bool checkSuccessful = true;
        int randomNumber = 6 * rnd.Next() + 1;
        // Kiểm tra trùng này !
        for (int j = 1; j < i - 1; j++)
        {
            // Also List1 is a collection?
            if (randomNumber == List1[j])
            {
                // Never use "GOTO"! use a variable to check.
                checkSuccessful = false;
                // Not nice but this is the only short solution I found for.
                break;
            }
        }
        // Check if the inner-loop-check was not failing.
        if (checkSuccessful)
        {
            List1[i] = randomNumber;
        }
    }
}




希望这会有所帮助.

致以诚挚的问候,祝您编程愉快,
停止




Hope this helps.

Best regards and happy coding,
Stops


这是您的C#代码:

Here is your C# code:

int i = 0;
int j = 0;
int Rand = 0;
ClicksMouse = 0;
for (i = 1; i <= 6; i++) {
    Radombutton(i).Image = null;
}

for (i = 1; i <= 6; i++) {
    A1:
    VBMath.Randomize();
    Rand = Conversion.Int(6 * VBMath.Rnd() + 1);

    //Kiểm tra trùng này !
    for (j = 1; j <= i - 1; j++) {
        if (Rand == List1(j))
            goto A1;
    }
    List1(i) = Rand;
}


这篇关于如何将此代码(VB)转换为C#?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆