选择条件案例(经典ASP)

时间:2017-04-04 17:38:24

标签: vbscript asp-classic select-case

<%
i=2 
  Select Case i
  Case 1,2,3,4,5,7,8,9,10
    response.write("Grade A")
  Case 11,12,13,14,15,16,17,18,19,20  
    response.write("Grade B")
  Case 21,22,23,24,25,26,27,28,29,30
    response.write("Grade C")
  Case 31,32,33,34,35,36,37,38,39,40
    response.write("Grade D")
  Case Else
    response.write("Invalid")
  End Select
  %> 

如何用较小的声明替换1,2,3 ..我尝试过1到10但是显示错误。

感谢任何帮助,谢谢!

3 个答案:

答案 0 :(得分:2)

一些替代方案

grade = "Invalid"
Select Case True
    Case i < 1   
    Case i < 11 : grade = "Grade A"
    Case i < 21 : grade = "Grade B"
    Case i < 31 : grade = "Grade C"
    Case i < 41 : grade = "Grade D"
End Select 

If i > 0 And i < 41 Then 
    grade = "Grade " + Chr(Asc("A") + (i-1)\10)
Else 
    grade = "Invalid"
End If 

If i > 0 And i < 41 Then 
    grade = Array("Grade A", "Grade B", "Grade C", "Grade D")((i-1)\10)
Else 
    grade = "Invalid"
End If 

已修改 以更正Ekkehard.Horner指出的错误

答案 1 :(得分:0)

由于您的规范归结为常规/ 可计算映射,Select Case If .. ElseIf - 即樱桃采摘 - 无论如何都是错误的工具:

Option Explicit

Dim i

For i = -1 to 42
    If 1 > i Or 40 < i Then
       WScript.Echo i, "invalid"
    Else
       WScript.Echo i, Chr(65 + ((i - 1) \ 10))
    End If
Next

输出:

cscript 43214055.vbs
-1 invalid
0 invalid
1 A
2 A
3 A
4 A
5 A
6 A
7 A
8 A
9 A
10 A
11 B
12 B
13 B
14 B
15 B
16 B
17 B
18 B
19 B
20 B
21 C
22 C
23 C
24 C
25 C
26 C
27 C
28 C
29 C
30 C
31 D
32 D
33 D
34 D
35 D
36 D
37 D
38 D
39 D
40 D
41 invalid
42 invalid

答案 2 :(得分:-1)

您在网页中使用了VBScript。它不支持Select Case语句中的范围。使用If/ElseIf语句可能会更好。

<%
    i=2
    If i > 0 And i <= 10 Then
        Response.Write("Grade A")
    ElseIf i > 10 And i <= 20 Then
        Response.Write("Grade B")
    ElseIf i > 20 And i <=30 Then
        Response.Write("Grade C")
    ElseIf i > 30 And i <= 40 Then
        Response.Write("Grade D")
    Else
        Response.Write("Invalid")
    End If
%>