如何使用开关和案例生成图片?

时间:2017-10-20 02:33:22

标签: javascript jquery html loops switch-statement

这是我到目前为止所拥有的。 function onclick() { var num = parseFloat(document.getElementById("do").value); if (num <= 0) { alert("Please enter a bet amount between 1 and 100"); } else if (num > 100) { alert("Please enter a bet amount between 1 and 100"); } var stake = document.getElementById("txtStake").value; if (stake < num) { alert("Wrong number Can't Be Higher Than Stake Number") } var random = newNumber(1, 7); if (random == 1) { document.getElementById("toChangeColor").style.backgroundColor = "red"; } else if (random == 2) { document.getElementById("toChangeColor").style.backgroundColor = "green"; } else if (random == 3) { document.getElementById("toChangeColor").style.backgroundColor = "gray"; } else if (random == 4) { document.getElementById("toChangeColor").style.backgroundColor = "yellow"; } else if (random == 5) { document.getElementById("toChangeColor").style.backgroundColor = "brown"; } else if (random == 6) { document.getElementById("toChangeColor").style.backgroundColor = "blue"; } else if (random == 7) { document.getElementById("toChangeColor").style.backgroundColor = "black"; } switch (random) { case 1: document.getElementById("picture").src = "time.jpg"; break; case 2: document.getElementById("picture").src = "pic2.jpg"; break; case 3: document.getElementById("picture").src = "pic3.jpg"; break; case 4: document.getElementById("picture").src = "pic4.jpg"; break; case 5: document.getElementById("picture").src = "pic5.jpg"; break; case 6: document.getElementById("picture").src = "pic6.jpg"; break; case 7: document.getElementById("picture").src = "pic7.jpg"; break; } } 是我做的最小和最大的功能。我真的遇到了麻烦。

#include <stdlib.h>
#include <stdio.h>
#include <ctype.h>

int isnumeric_array(char *string)
{
    while( (char)(*string) != '\0') {
        if( !isdigit( (char)(*string) ) ) {
            return 0;
        }
        string++;
    }

    return 1;
}

int main(){
    char grade[128];
    float grade2;

read_input:
    memset(grade, 0x0, sizeof(grade));
    printf("Please, insert a grade between 0 and 10.\n");
    printf("Grade: ");
    scanf("%s", grade);

    if(isnumeric_array(grade) == 0) {
        goto read_input;
    }

    grade2 = atof(grade);
    if((grade2 < 0) || (grade2 > 10)) {
        goto read_input;
    }
    else {
        printf("You have entered right value\n");
    }

    system("PAUSE");
}

当我点击一个按钮时,我需要在一个区域中显示5个选项中的随机图片。如何使用开关或循环执行此操作?

1 个答案:

答案 0 :(得分:0)

你写切换的情况或者如果是多余的情况,只需在数组或二维数组中制作就会更好。

var bgColors = ['red', 'green', 'yelow'];
var pics = ['time.jpg', 'pic2.jpg', 'pic3.jpg'];

var randomIndex = newNumber(0, 2);

document.getElementById("toChangeColor").style.backgroundColor = bgColors[randomIndex];
document.getElementById("picture").src = pics[randomIndex];