【问题标题】:find the greatest of 10 numbers in vbscript在 vbscript 中找到 10 个数字中的最大值
【发布时间】:2019-02-28 09:08:32
【问题描述】:

我刚刚创建了 vbscript 代码来从 10 个输入数字中找出最大的数字。它仅适用于 1 到 10 之间的数字。为什么?

<html>
<head>
    <title>Enter 10 numbers to find the largest number</title>
    <meta http-equiv="x-ua-compatible" content="IE=10">
</head>

<body>
<script language = "vbscript" type = "text/vbscript">
    Dim n1 
    Dim n2
    Dim n3
    Dim n4
    Dim n5
    Dim n6
    Dim n7
    Dim n8
    Dim n9
    Dim n10
    Dim largest
    n1=inputBox("please Enter the  number1 ","number1")
    n2=inputBox("please Enter the  number2 ","number2")
    n3=inputBox("please Enter the  number3 ","number3")
    n4=inputBox("please Enter the  number4 ","number4")
    n5=inputBox("please Enter the  number5 ","number5")
    n6=inputBox("please Enter the  number6 ","number6")
    n7=inputBox("please Enter the  number7 ","number7")
    n8=inputBox("please Enter the  number8 ","number8")
    n9=inputBox("please Enter the  number9 ","number9")
    n10=inputBox("please Enter the  number10 ","number10")

    If (n1>n2 AND num1>vnum3 AND n1>n4 AND n1>n5 AND n1>n6 AND n1>n7 AND n1>n8 AND n1>n9 AND n1>n10 ) then
         document.write("this is the largest number " & n1 & ", Enjoy VBscript")
    ElseIf (n2>n1 AND n2>n3 AND n2>n4 AND n2>n5 AND n2>n6 AND n2>n7 AND n2>n8 AND n2>n9 AND n2>n10 ) Then
         document.write("this is the largest number " & n2 & ", Enjoy VBscript")
    ElseIf (n3>n1 AND n3>n2 AND n3>n4 AND n3>n5 AND n3>n6 AND n3>n7 AND n3>n8 AND n3>n9 AND n3>n10 ) Then
         document.write("this is the largest number " & n3 & ", Enjoy VBscript")
    ElseIf (n4>n1 AND n4>n3 AND n4>n2 AND n4>n5 AND n4>n6 AND n4>n7 AND n4>n8 AND n4>n9 AND n4>n10 ) Then
         document.write("this is the largest number " & n4 & ", Enjoy VBscript")
    ElseIf (n5>n1 AND n5>n3 AND n5>n4 AND n5>n2 AND n5>n6 AND n5>n7 AND n5>n8 AND n5>n9 AND n5>n10 ) Then
         document.write("this is the largest number " & n5 & ", Enjoy VBscript")
    ElseIf (n6>n1 AND n6>n3 AND n6>n4 AND n6>n5 AND n6>n2 AND n6>n7 AND n6>n8 AND n6>n9 AND n6>n10 ) Then
         document.write("this is the largest number " & n6 & ", Enjoy VBscript")
    ElseIf (n7>n1 AND n7>n3 AND n7>n4 AND n7>n5 AND n7>n6 AND n7>n2 AND n7>n8 AND n7>n9 AND n7>n10 ) Then
         document.write("this is the largest number " & n7 & ", Enjoy VBscript")
    ElseIf (n8>n1 AND n8>n3 AND n8>n4 AND n8>n5 AND n8>n6 AND n8>n7 AND n8>n2 AND n8>n9 AND n8>n10 ) Then
         document.write("this is the largest number " & n8 & ", Enjoy VBscript")
    ElseIf (n9>n1 AND n9>n3 AND n9>n4 AND n9>n5 AND n9>n6 AND n9>n7 AND n9>n8 AND n9>n2 AND n9>n10 ) Then
         document.write("this is the largest number " & n9 & ", Enjoy VBscript")
    ElseIf (n10>n1 AND n10>n3 AND n10>n4 AND n10>n5 AND n10>n6 AND n10>n7 AND n10>n8 AND n10>n9 AND n10>n2 ) Then
         document.write("this is the largest number " & n10 & ", Enjoy VBscript")
    Else
         document.write("this is the largest number is  " & number10 & ", Enjoy VBscript")
    End If
</script>
</body>
</html>

【问题讨论】:

  • 最简单的方法是在内存中只保留两个数字:前一个较大的值和新读取的值。如果读取的数字大于前一个较大的值,则覆盖前一个值,否则丢弃它并要求另一个数字。
  • 因为您要比较的是字符串,而不是数字。用字符串比较 "5" > "10"
  • 不确定你问用户10次的确切要求(输入框),但你可以看看this解决方案

标签: vbscript


【解决方案1】:

这会将事物放入循环中并使用排序的对象。

'Getting the 10 numbers
Dim Thing(10)
For x = 0 to 9
    Thing(x) = InputBox("Something")
Next

Set rs = CreateObject("ADODB.Recordset")
With rs
    'Setting up a one field database containing a single precision field called Numbers
    .Fields.Append "Number", 4 
    .Open

    'Adding the data
    For x = 0 to 9
        .AddNew
        .Fields("Number").value = Thing(x)
        .UpDate
    Next

    'Sorting descending on number field
    .Sort = "Number DESC"

    'Now write it
    MsgBox .Fields("Number").Value
    'And the second one
    .MoveNext
    MsgBox .Fields("Number").Value

    'And all the rest
    For x = 0 to 7
        .MoveNext
        MyStr = MyStr & .Fields("Number").Value & vbcrlf
    Next
    MsgBox MyStr
End With

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-01-08
    • 1970-01-01
    • 1970-01-01
    • 2018-05-23
    • 2016-04-24
    • 1970-01-01
    • 2021-12-11
    • 1970-01-01
    相关资源
    最近更新 更多