首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >当用户尝试减去时,如果没有更多要减去的东西,则提醒用户

问当用户尝试减去时,如果没有更多要减去的东西,则提醒用户
EN

Stack Overflow用户
提问于 2018-10-08 01:53:57
回答 1查看 51关注 0票数 1

我有一个有两个按钮的页面:

  • One将1个单位的物品添加到用户的库存中。
  • One从用户的库存中删除该物品的1个单位。在这个例子中,这个东西将是钻石。

每次用户拿起或丢下钻石时,都会有一条消息告诉他总共有多少钻石。

我不希望这些消息过于泛泛,比如:“你有1个菱形(S)”。

我设法生成了不同的消息,这取决于用户拥有的菱形数量,以及他是在拿起它们,还是在丢弃它们:

我也希望有自定义的消息,当用户试图拾起一个钻石时,他的钱包已经满了,当他试图丢弃一个时,他没有任何钻石下降。例如:

“你不能再摘钻石了,你的钱包已经满了。”和“你没有任何钻石可丢弃。”

这就是我需要帮助的地方。

正如您将在下面的代码中看到的,我让diamondCount == 0在函数中放置菱形,以生成消息"You drop your last菱形“。如果用户试图从这一点放入另一个菱形,diamondCount仍将为0。那么,当用户刚刚到达0,以及当他试图从0开始丢弃菱形时,我如何区分它呢?

这就是我所拥有的:

​

代码语言:javascript
复制
        function pickDiamond() {
            if (typeof(Storage) !== "undefined") {
                if (localStorage.diamondCount < 10) {
                    localStorage.diamondCount = Number(localStorage.diamondCount) + 1;
                } else {
                    localStorage.diamondCount = 10;
                }
                if (localStorage.diamondCount == 1) {
                    document.getElementById("result").innerHTML = "You picked up your first Diamond!";
                } else {
                    document.getElementById("result").innerHTML = "You picked up a Diamond. You now have a total of " + localStorage.diamondCount + " Diamonds in your wallet.";
                }
            }
        }

        function dropDiamond() {
            if (typeof(Storage) !== "undefined") {
                if (localStorage.diamondCount > 0) {
                    localStorage.diamondCount = Number(localStorage.diamondCount) - 1;
                } else {
                    localStorage.diamondCount = 0;
                }
                if (localStorage.diamondCount == 1) {
                    document.getElementById("result").innerHTML = "You dropped 1 Diamond. You only have 1 Diamond left in your wallet.";
                } else if (localStorage.diamondCount == 0) {
                    document.getElementById("result").innerHTML = "You dropped your last Diamond. You don't have any Diamonds now.";
                } else {
                    document.getElementById("result").innerHTML = "You dropped a Diamond. You now have a total of " + localStorage.diamondCount + " Diamonds in your wallet.";
                }
            }
        }

        function dropAllDiamonds() {
            localStorage.diamondCount = 0;
            document.getElementById("result").innerHTML = "You dropped all your Diamonds.";
        }
代码语言:javascript
复制
    <p>Use the buttons to pick up or drop Diamonds</p>
    <p><button onclick="pickDiamond()" type="button">Pick up Diamonds</button></p>
    <p><button onclick="dropDiamond()" type="button">Drop Diamonds</button></p>
    <p><button onclick="dropAllDiamonds()" type="button">Drop All Diamonds</button></p>
    <p id="result"></p>

​

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-10-08 04:32:06

看起来你的问题是if/else逻辑的问题之一。具体地说,你应该能够用一个if/else链做任何你想做的事情。我对你的代码做了一些修改,如下所示:

对于pickDiamond,我从钻石计数为零开始,并随着用户库存的增加在逻辑序列中添加新的文本。只处理数字,增量运算符在这里比较合适。一旦用户的库存达到10,逻辑将默认为“您不能再摘钻石了。您的钱包已经满了。”最后的else语句。为了测试,我创建了一个localStorage对象。

代码语言:javascript
复制
    let localStorage = {
        diamondCount: 0
    }
    function pickDiamond() {
        if (typeof(Storage) !== "undefined") {
            if (localStorage.diamondCount == 0) {
                localStorage.diamondCount++;
                document.getElementById("result").innerHTML = "You picked up your first Diamond!";
            } else if (localStorage.diamondCount < 10) {
                localStorage.diamondCount++;
                document.getElementById("result").innerHTML = "You picked up a Diamond. You now have a total of " + localStorage.diamondCount + " Diamonds in your wallet.";
            } else {
                document.getElementById("result").innerHTML = "You cannot pick any more diamonds. Your wallet is already full.";
            }

            }
        }

类似的解决方案也可以用于dropDiamond函数,但反之亦然。dropAllDiamonds函数不需要改动就能正常工作。

代码语言:javascript
复制
        function dropDiamond() {
        if (typeof(Storage) !== "undefined") {
            if (localStorage.diamondCount > 2) {
                localStorage.diamondCount--;
                document.getElementById("result").innerHTML = "You dropped a Diamond. You now have a total of " + localStorage.diamondCount + " Diamonds in your wallet.";                 
            } else  if (localStorage.diamondCount == 2) {
                localStorage.diamondCount--;
                document.getElementById("result").innerHTML = "You dropped 1 Diamond. You only have 1 Diamond left in your wallet.";                    
            } else if (localStorage.diamondCount == 1) {
                localStorage.diamondCount--;
                document.getElementById("result").innerHTML = "You dropped your last Diamond. You don't have any Diamonds now.";
            } else {
                document.getElementById("result").innerHTML = "You don't have any Diamonds to drop ya dingus";
            }
        }
    }
        function dropAllDiamonds() {
             localStorage.diamondCount = 0;
             document.getElementById("result").innerHTML = "You dropped all your Diamonds.";
    }
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/52691272

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档