您如何使else语句什么都不做? [英] How do you make your else statement do nothing?

查看:780
本文介绍了您如何使else语句什么都不做?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是JavaScript编程的新手,我需要帮助! 我正在制作一个小程序,该程序随机选择一个数字(1-4),然后根据该数字打印出一条语句. A = 1、2 = B,3 = C和4 = D.我的问题是,对于我的if/else语句,我希望else语句不执行任何操作.如何? (很抱歉,如果这是一个愚蠢的问题,我是JS新手)代码是:

I'm new to programming in JavaScript, and I need help! I am making a small program that randomly selects a number (1-4) and then prints out a statement based on the number. A=1, 2=B, 3=C, and 4=D. My problem is that with my if/else statements, I want the else statement to do nothing. How? (Sorry if this is a dumb question, I'm new to JS) The code is:

 var randomGuess=Math.floor(Math.random()*4)+1);
            if (randomGuess===1) {
                document.write("Try A."); }
                else { 
                return false }
            if (randomGuess===2) {
                document.write("Try B.") }
                else {
                return false }
            if (randomGuess===3) {
                document.write("Try C.") } 
                else {
                return false }
            if (randomGuess===4) {
                document.write("Try D.") } 
                else {
                return false }

推荐答案

if始终不需要else ..因此将其删除.

an if doesn't need an else always .. so remove it.

if(){ ...}
if(){ ...}
if(){ ...} 

很好,但是建议在这种情况下使用switch.

is fine , but its adviced to use switch in this case.

switch 效果更好在这种情况下

switch works better in this kind of situation

这篇关于您如何使else语句什么都不做?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆