簡體   English   中英

Switch()不適用於Math.random()數字

[英]Switch() does not work with Math.random() numbers

我正在嘗試制作一個涉及switch()的函數,並提供有關隨機生成的特定數字的答案。 在某種情況下,我的函數無法運行。 無論數字是多少,它只給我默認情況。

這是我的代碼:

var i;
var girl;

function response() {
    var girl = prompt("What girl do you like?");
    var r = (Math.random() * (3 - 1 + 1) + 1).toFixed(0);
    var i = r;

    switch(i) {
        case (i == 1):
            alert(girl + " likes you as a friend.");
            break;

        case (i == 2):
            alert(girl + " does not really like you.");
            break;

        case (i == 3):
            alert(girl + " has a crush on you.");
            break;

        case (i == 4):
            alert(girl + " wants you to ask her out.");
            break;

        default:
            console.log(i);
    }
}

那不是開關的工作方式。 它將每種case的值與switch進行比較。

現在基本上是將i的值與布爾值進行多次比較(例如i == 1 )。

同樣,通過將帶有靜態值的算術添加到值中,您的隨機性不會像您一樣變得更加隨機 您應該將其替換為4 您還應該使用Math.ceil()類的東西(因為您忽略了0值,這也不是一個好主意),而不要使用toFixed()來返回字符串。

您也不需要括號之間的值進行比較。 如果您知道隨機數的范圍,則可能也不需要默認大小寫(因為您已經涵蓋了所有可能性)。

您也可以直接使用r ,無需重新分配。

您的變量也是函數的局部變量,因此可能不需要它們的頂部。

這是我將其重寫的方式:

function response() {
    var girl = prompt("What girl do you like?");
    var r = Math.floor(Math.random() * 4);

    switch(r) {
        case 0:
            alert(girl + " likes you as a friend.");
            break;

        case 1:
            alert(girl + " does not really like you.");
            break;

        case 2:
            alert(girl + " has a crush on you.");
            break;

        case 3:
            alert(girl + " wants you to ask her out.");
            break;

    }
}

...甚至...

function response() {
    var answerSuffixes = [
        " likes you as a friend.",
        " does not really like you.",
        " has a crush on you.",
        " wants you to ask her out."
    ];
    var girl = prompt("What girl do you like?");
    var r = Math.floor(Math.random() * answerSuffixes.length);

    alert(girl + answerSuffixes[r]);
}

Javascript中的case語句已經在目標i上進行了匹配:

switch(i) {
        case 1:
            alert(girl + " likes you as a friend.");
            break;

        case 2:
            alert(girl + " does not really like you.");
            break;

        case 3:
            alert(girl + " has a crush on you.");
            break;

        case 4:
            alert(girl + " wants you to ask her out.");
            break;

        default:
            console.log(i);
    }

因此,在每種case都不需要具有比較表達式。

switch比較嚴格。

你需要

switch (i) {
    case 1:
        alert(girl + " likes you as a friend.");
        break;

要么

switch (true) {
    case i === 1:
        alert(girl + " likes you as a friend.");
        break;

聲明開關時,將使用的變量與每種情況進行比較,因此不需要評估。

同樣,為了保持變量類型的檢查,可能有助於將“ r”的答案解析為整數。

function response() {
    var girl = prompt("What girl do you like?");
    var r = parseInt((Math.random() * (3 - 1 + 1) + 1).toFixed(0));
    var i = r;

    switch(i) {
        case 1:
            alert(girl + " likes you as a friend.");
            break;

        case 2:
            alert(girl + " does not really like you.");
            break;

        case 3:
            alert(girl + " has a crush on you.");
            break;

        case 4:
            alert(girl + " wants you to ask her out.");
            break;

        default:
            console.log(i);
   }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM