1

我正在制作一个应用程序,其中随机抛出 4 个筹码。我完成了通过单击按钮生成随机数,随机数在 1 到 4 之间生成。

我的问题是如何将 4 张图片与这些数字相关联?假设如果 1 随机出现 - 那么应该显示 1.jpg。我的编程语言是 c#,在 Visual Studio 2012 中工作。

public sealed partial class MainPage : Page

{

   Random rand = new Random();

 public MainPage()
    {

        this.InitializeComponent();
    }

    protected override void OnNavigatedTo(NavigationEventArgs e)
    {
    }

    private void Button_Click_1(object sender, RoutedEventArgs e)
    {
        int num=rand.Next(1,5);
        textblock1.Text=num.ToString();
    }}}

我在文本块中显示数字。现在我有 4 张图像必须随机显示。我的问题是如何以及在何处显示它们。

4

5 回答 5

3

完成使用此方法

private void Button_Click_1(object sender, RoutedEventArgs e)

    {

        Random rand = new Random();
        int dice = rand.Next(1, 5);

        switch (dice)
        {
            case 1:

                Image1.Source = new BitmapImage(new Uri(@"ms-appx:///Assets/1.png"));

                break;
            case 2:

                two.Source = new BitmapImage(new Uri(@"ms-appx:///Assets/2.png"));
                break;
            case 3:

                three.Source = new BitmapImage(new Uri(@"ms-appx:///Assets/3.png"));
                break;
            case 4:

                four.Source = new BitmapImage(new Uri(@"ms-appx:///Assets/4.png"));
                break;

        }

    }for bitmap image refrence will be "using Windows.UI.Xaml.Media.Imaging;"
于 2013-03-24T04:20:58.203 回答
1

您可以将生成的数字替换为文件名,例如:

string.Format("{0}.jpg", generatedNumber);

非常少的代码,不需要“if”或“switch”。

我还将制作一个单独的组件 (I)FileNameResolver,其中包含确定正确文件名和错误处理的逻辑;并在你的代码中使用那个。

于 2013-03-12T17:42:07.140 回答
0

关于什么:

Random rand = new Random();

Bitmap GetRandomImage(){
    Bitmap bmp;
    int n = rand.Next(0, 4);

    if(n == 0)
       bmp = new Bitmap("mypath1");
    else if(n == 1)
       bmp = new Bitmap("mypath2");
    else if(n == 2)
       bmp = new Bitmap("mypath3");
    else
       bmp = new Bitmap("mypath4");

    return bmp;
}

或者您可以重命名文件并使用以下内容:

Random rand = new Random();

Bitmap GetRandomImage(){
    int n = rand.Next(0, 4);

    return new Bitmap(string.Format("mypath\\{0}.jpg", n));
}
于 2013-03-12T17:44:03.057 回答
0

随机 rand = new Random(); int randomPosition = rand.Next(0, 9);

            lblEnglish.Text = sw.EnglishHeartSentence[randomPosition];
            lblShona.Text = sw.shonaSentence[randomPosition];
            switch (randomPosition)
            {
                case 0:
                    mainPicBox.Image = Funda_isizulu_.Properties.Resources.diarrhea_900x600px;
                    break;
                case 1:
                    mainPicBox.Image = Funda_isizulu_.Properties.Resources.hunger1; ;
                    break;
                case 2:
                    mainPicBox.Image = Funda_isizulu_.Properties.Resources.stomachache1;
                    break;
                case 3:
                    mainPicBox.Image = Funda_isizulu_.Properties.Resources.full_clipart_1;
                    break;
                case 4:
                    mainPicBox.Image = Funda_isizulu_.Properties.Resources.Top_10_Foods_To_Relieve_Menstrual_Cramps_1;
                    break;
                case 5:
                    mainPicBox.Image = Funda_isizulu_.Properties.Resources.preg;
                    break;
                case 6:
                    mainPicBox.Image = Funda_isizulu_.Properties.Resources.belly;
                    break;
                case 7:
                    mainPicBox.Image = Funda_isizulu_.Properties.Resources.a_man_with_visible_spine_holds_his_back;
                    break;
                case 8:
                    mainPicBox.Image = Funda_isizulu_.Properties.Resources.bell;
                    break;
                case 9:
                    mainPicBox.Image = Funda_isizulu_.Properties.Resources.coffee_growl_905;
                    break;

            }
于 2018-06-28T23:32:55.727 回答
-1

呃,

switch (number)
{
    case 1:
        return "1.jpg";
    case 2:
        return "2.jpg";
    case 3:
        return "3.jpg";
    case 4:
        return "4.jpg";
    default:
        throw ArgumentException("number");
}

这一切都取决于,

怎么样,

private static readonly WhateverTypeMyImagesNeedToBe[] images =
    {
        new WhateverTypeMyImagesNeedToBe("1.jpg"),
        new WhateverTypeMyImagesNeedToBe("2.jpg"),
        new WhateverTypeMyImagesNeedToBe("3.jpg"),
        new WhateverTypeMyImagesNeedToBe("4.jpg")
    }

private static WhateverTypeMyImagesNeedToBe GetRandomImage()
{
    return images[new Random().Next(0, images.Length)];
}
于 2013-03-12T17:43:02.150 回答