简体   繁体   English

Javascript switch 语句不起作用

[英]Javascript switch statement does not work

This code works perfectly.此代码完美运行。 But if I uncomment the switch statement, it fails completely.但是如果我取消注释 switch 语句,它就会完全失败。 The switch statement seems to be correct. switch 语句似乎是正确的。 Any help will be greatly appreciated.任何帮助将不胜感激。

 function draw(n) { let canvas = document.querySelector('#my-canvas'); let context = canvas.getContext('2d'); context.fillStyle = 'black' let contourInterval = 1500 let contourWidth = 500 let fsize = 1000 let x0 = fsize / 2 let y0 = fsize / 2 for (j = 0, y = -y0; j < fsize; j++, y++) { for (i = 0, x = -x0; i < fsize; i++, x++) { /* switch(n) { case 0: let fun = (x*x + y*y) break; case 1: let fun = (x*x - y*y) break; case 2: let fun = 1.0/(x*x + y*y) break; default: let fun = (x*x + y*y) } */ let fun = (x * x + y * y) if (Math.abs(fun % contourInterval) < contourWidth) { context.fillRect(x + x0 / 2, y + y0 / 2, 1, 1) } } } } draw(1)
 <canvas id="my-canvas" width="500" height="500"></canvas>

The problem is that you're declaring the fun variable inside the switch statement and then trying to use it outside later.问题是您在switch语句中声明了fun变量,然后稍后尝试在外部使用它。

Try updating your code to the following:尝试将您的代码更新为以下内容:

 function draw(n) { let canvas = document.querySelector('#my-canvas'); let context = canvas.getContext('2d'); context.fillStyle = 'black' let contourInterval = 1500 let contourWidth = 500 let fsize = 1000 let x0 = fsize / 2 let y0 = fsize / 2 for (j = 0, y = -y0; j < fsize; j++, y++) { for (i = 0, x = -x0; i < fsize; i++, x++) { let fun = 0 // declare this here outside the scope of the switch statement switch(n) { case 0: // remember to get rid of the `let` keywords in your `case` statments fun = (x*x + y*y) break; case 1: fun = (x*x - y*y) break; case 2: fun = 1.0/(x*x + y*y) break; default: fun = (x*x + y*y) } // now `fun` can be used here without scope problems if (Math.abs(fun % contourInterval) < contourWidth) { context.fillRect(x + x0 / 2, y + y0 / 2, 1, 1) } } } } draw(1)
 <canvas id="my-canvas" width="500" height="500"></canvas>

This way you declare the variable in the outer scope and use the switch statement to set it.这样你就在外面声明了变量 scope 并使用switch语句来设置它。 Now it can be used in the outer scope.现在可以在外层scope中使用了。

Declare fun first先声明好玩

 function draw(n) { let canvas = document.querySelector('#my-canvas'); let context = canvas.getContext('2d'); context.fillStyle = 'black' let contourInterval = 1500 let contourWidth = 500 let fsize = 1000 let x0 = fsize / 2 let y0 = fsize / 2 for (j = 0, y = -y0; j < fsize; j++, y++) { for (i = 0, x = -x0; i < fsize; i++, x++) { let fun = 0 switch(n) { case 0: fun = (x*x + y*y) break; case 1: fun = (x*x - y*y) break; case 2: fun = 1.0/(x*x + y*y) break; default: fun = (x*x + y*y) } if (Math.abs(fun % contourInterval) < contourWidth) { context.fillRect(x + x0 / 2, y + y0 / 2, 1, 1) } } } } draw(1)
 <canvas id="my-canvas" width="500" height="500"></canvas>

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

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