切换语句 javascript
switch statemen t javascript
我制作了这段代码,但它根本不起作用
我应该使用 switch 语句
你能告诉我错误在哪里吗
非常感谢 :)
var side == parseInt(prompt('Enter a number of side between 3 and 10: '));
var shape ==['triangle','square','pentagon','hexagon','heptagon','octagon','nonagon','octagon'];
switch (shape){
case== 3:
shape=[0];
break;
case== 4:
shape==[1];
break;
case== 5:
shape==[2];
break;
case== 6:
shape==[3];
break;
case== 7:
shape==[4];
break;
case== 8:
shape==[5];
break;
case== 9:
shape==[6];
break;
case== 10:
shape==[7];
break;
}
alert('The shape is' + shape);
您正在创建一个数组 shape=[0];
未引用具有值的数组。
您有很多语法错误:
case== 3:
应该只是
case 3:
和
shape==[1];
==
测试质量,所以你基本上是在说 shape is the same as an array containing the number 1
。
当您应该使用 =
时,您却使用了 ==
。在您的开关中,您想检查用户输入(side
),然后大小写只需要选项没有符号。最后形状需要重新分配给数组中的值。
var side = parseInt(prompt('Enter a number of side between 3 and 10: '));
var shape =['triangle','square','pentagon','hexagon','heptagon','octagon','nonagon','octagon'];
switch (side){
case 3:
shape=shape[0];
break;
case 4:
shape=shape[1];
break;
case 5:
shape=shape[2];
break;
case 6:
shape=shape[3];
break;
case 7:
shape=shape[4];
break;
case 8:
shape=shape[5];
break;
case 9:
shape=shape[6];
break;
case 10:
shape=shape[7];
break;
}
alert('The shape is ' + shape);
停止使用 ==
代替 =
。
==
是比较校验,=
是赋值运算符。
使用开关:
var side = parseInt(prompt('Enter a number of side between 3 and 10')),
shapes = ['triangle','square','pentagon','hexagon','heptagon','octagon','nonagon','octagon'],
shape;
switch (side){
case 3:
shape = shapes[0];
break;
case 4:
shape = shapes[1];
break;
case 5:
shape = shapes[2];
break;
case 6:
shape = shapes[3];
break;
case 7:
shape = shapes[4];
break;
case 8:
shape = shapes[5];
break;
case 9:
shape = shapes[6];
break;
case 10:
shape = shapes[7];
break;
}
alert('The shape is ' + shape);
更好的方法:
var side = parseInt(prompt('Enter a number of side between 3 and 10')),
shapes = ['triangle','square','pentagon','hexagon','heptagon','octagon','nonagon','octagon'];
alert('The shape is' + shapes[side - 3]);
您不需要 switch 语句 - 您可以拥有更短更简洁的代码。您可以只分配形状,但请确保检查提示的边是否在范围内。
var shape = ['triangle','square','pentagon','hexagon','heptagon','octagon','nonagon','decagon'];
var side = parseInt(prompt('Enter a number of side between 3 and 10: '));
var userShape = shape[side - 3];
if (side < 3 || side > 10) {
alert('Not a recognised shape')
} else {
alert(userShape);
}
除非您确定不会再次使用 shape
数组,否则最好将检查数组的结果分配给一个新变量(此处为 userShape
),否则您只是覆盖数组。
另外,十边形是十边形:)
我制作了这段代码,但它根本不起作用 我应该使用 switch 语句 你能告诉我错误在哪里吗 非常感谢 :)
var side == parseInt(prompt('Enter a number of side between 3 and 10: '));
var shape ==['triangle','square','pentagon','hexagon','heptagon','octagon','nonagon','octagon'];
switch (shape){
case== 3:
shape=[0];
break;
case== 4:
shape==[1];
break;
case== 5:
shape==[2];
break;
case== 6:
shape==[3];
break;
case== 7:
shape==[4];
break;
case== 8:
shape==[5];
break;
case== 9:
shape==[6];
break;
case== 10:
shape==[7];
break;
}
alert('The shape is' + shape);
您正在创建一个数组 shape=[0];
未引用具有值的数组。
您有很多语法错误:
case== 3:
应该只是
case 3:
和
shape==[1];
==
测试质量,所以你基本上是在说 shape is the same as an array containing the number 1
。
当您应该使用 =
时,您却使用了 ==
。在您的开关中,您想检查用户输入(side
),然后大小写只需要选项没有符号。最后形状需要重新分配给数组中的值。
var side = parseInt(prompt('Enter a number of side between 3 and 10: '));
var shape =['triangle','square','pentagon','hexagon','heptagon','octagon','nonagon','octagon'];
switch (side){
case 3:
shape=shape[0];
break;
case 4:
shape=shape[1];
break;
case 5:
shape=shape[2];
break;
case 6:
shape=shape[3];
break;
case 7:
shape=shape[4];
break;
case 8:
shape=shape[5];
break;
case 9:
shape=shape[6];
break;
case 10:
shape=shape[7];
break;
}
alert('The shape is ' + shape);
停止使用 ==
代替 =
。
==
是比较校验,=
是赋值运算符。
使用开关:
var side = parseInt(prompt('Enter a number of side between 3 and 10')),
shapes = ['triangle','square','pentagon','hexagon','heptagon','octagon','nonagon','octagon'],
shape;
switch (side){
case 3:
shape = shapes[0];
break;
case 4:
shape = shapes[1];
break;
case 5:
shape = shapes[2];
break;
case 6:
shape = shapes[3];
break;
case 7:
shape = shapes[4];
break;
case 8:
shape = shapes[5];
break;
case 9:
shape = shapes[6];
break;
case 10:
shape = shapes[7];
break;
}
alert('The shape is ' + shape);
更好的方法:
var side = parseInt(prompt('Enter a number of side between 3 and 10')),
shapes = ['triangle','square','pentagon','hexagon','heptagon','octagon','nonagon','octagon'];
alert('The shape is' + shapes[side - 3]);
您不需要 switch 语句 - 您可以拥有更短更简洁的代码。您可以只分配形状,但请确保检查提示的边是否在范围内。
var shape = ['triangle','square','pentagon','hexagon','heptagon','octagon','nonagon','decagon'];
var side = parseInt(prompt('Enter a number of side between 3 and 10: '));
var userShape = shape[side - 3];
if (side < 3 || side > 10) {
alert('Not a recognised shape')
} else {
alert(userShape);
}
除非您确定不会再次使用 shape
数组,否则最好将检查数组的结果分配给一个新变量(此处为 userShape
),否则您只是覆盖数组。
另外,十边形是十边形:)