简体   繁体   English

C#中的此switch语句有什么问题?

[英]What is wrong with this switch statement in C#?

I am trying to take a value from a user then every click on the button it must print statement and a picture that represent a number of trying. 我试图从用户那里获取一个值,然后每次单击按钮都必须打印语句和一张代表许多尝试的图片。 The if statement is working but it always shows the pic of last case. if语句有效,但始终显示最后一种情况的图片。

private void button1_Click(object sender, EventArgs e)
{
    if (textBox1.Text != "")
    {    
        int gussednum= Convert.ToInt16( textBox1.Text); 

        for (int i = 0; i < 7; ++i)
        {
                if (interednum < num)
                {
                    label1.Text = "Should be greater than" + num;
                    switch (i)
                    {
                        case (0) :
                            Image image = Image.FromFile("1.png");
                            pictureBox1.Image = image;
                            break;

                        case (1):
                            Image im2 = Image.FromFile("2.png");
                            pictureBox1.Image = im2;
                            break;

                        case (2):
                            Image im3 = Image.FromFile("3.png");
                            pictureBox1.Image = im3;
                            break;

                        case (3):
                            Image image4 = Image.FromFile("4.png");
                            pictureBox1.Image = image4;
                            break;

                        case (4):
                            Image image5 = Image.FromFile("5.png");
                            pictureBox1.Image = image5;
                            break;

                        case (5):
                            Image image6 = Image.FromFile("dead.gif");
                            pictureBox1.Image = image6;
                            break;

                        case (6):
                            Image image7 = Image.FromFile("red.png");
                            pictureBox1.Image = image7;
                            break;
                    }
                }

You always assign an image to pictureBox1. 您始终将图像分配给pictureBox1。 And obviously the last time in the loop the last switch/case is executed (#7) 很明显,循环中的最后一次执行了最后一个开关/案例(#7)

pictureBox1.Image = image5;

So, you either mistyped pictureBox1 on copy/pasting, or you should break a loop as well (from your switch/case break). 因此,您要么在复制/粘贴时错误输入了pictureBox1,要么也应该中断循环(从开关/大小写中断开始)。

I am not sure if that's even StackOverflow question but just some copy/paste issue. 我不确定这是否是StackOverflow问题,而只是一些复制/粘贴问题。

Since you've wrapped the switch/case with this for loop : 由于您已经使用for loop包装了switch/case

for (int i = 0; i < 7; ++i)

Whenever, the following is true : 无论何时,以下是true

interednum < num

You'll always have i equal to 6 because it loops to it every run. 您将始终拥有等于6 i ,因为它每次运行都会循环到它。 This is why it'll always output the last case . 这就是为什么它将始终输出最后case

This may or may not be an issue, but worth looking at. 这可能是问题,也可能不是问题,但值得一看。 It seems you always change the same object ( pictureBox1 ): 看来您总是更改同一对象( pictureBox1 ):

pictureBox1.Image = image7;

You may have more than one pictureBox object, so it may look like this instead: 您可能有多个pictureBox对象,因此它看起来像这样:

pictureBox7.Image = image7;//do this for every pictureBox in every case statement

It's run in loop so it's insert first image, next second and so on until the last. 它循环运行,因此将插入第一个图像,然后插入第二个图像,依此类推,直到最后一个图像。 Possible it's to fast to see it and that's why u see only last image. 可能很快就会看到它,这就是为什么您只能看到最后一张图像。 Try to debug that program to see it's true. 尝试调试该程序以查看它是正确的。

My advice: just change all break's commands for "return" in switch case. 我的建议:在切换情况下,只需将所有break的命令更改为“ return”即可。 That's all. 就这样。

Ps. PS。 This code doesn't have sense in my opinion (i saying about switch case in for loop ;)). 我认为这段代码没有意义(我说的是for循环中的switch情况;)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM