【问题标题】:I get a type mismatch error我收到类型不匹配错误
【发布时间】:2015-10-21 20:55:40
【问题描述】:

我正在做一个项目来提高我计算四分卫传球手评分的逻辑技能。我已经尝试了所有技巧来调试这个问题,但我仍然不知所措。首先,我将向您展示我的代码。

'Prompt Statements
'error handling to see if the previous 5 prompted inputs are numbers

Wscript.StdOut.WriteLine "Choose a Quarterback : "
QB = Wscript.StdIn.ReadLine

'attempts and completions loop
'attempts

do
  Wscript.StdOut.WriteLine "How many attempts did " & QB & " throw: "
  attempts = Wscript.StdIn.ReadLine
  if IsNumeric(attempts) then
    attempts = CInt(attempts)
  else
    Wscript.StdOut.Write "You did not enter a number. Please try again."
  end if
  loop while IsNumeric(attempts) = false

  'completions
do
  do
    Wscript.StdOut.WriteLine "How many completed passes did " & QB & " throw for: "
    completions = Wscript.StdIn.ReadLine
    if IsNumeric(completions) then
      completions = CInt(completions)
    else
      Wscript.StdOut.Write "You did not enter a number. Please try again."
    end if
  loop while IsNumeric(completions) = false
  if attempts < completions then
    Wscript.StdOut.Writeline "Completions can not be more that attempts please try again."
  else
    exit do
  end if
loop while attempts < completions

'yards
do
  Wscript.StdOut.WriteLine "How many yards did " & QB & " throw for: "
  yards = Wscript.StdIn.ReadLine
  if IsNumeric(yards) then
    if yards <= 32767 then
      yards = CInt(yards)
      exit do
    else
      if yards > 32767 then
        yards = CLng(yards)
        exit do
      end if
    end if
  else
    Wscript.StdOut.Write "You did not enter a number. Please try again."
  end if
loop while IsNumeric(yards) = False

'touchdowns
do
  Wscript.StdOut.WriteLine "How many touchdowns did " & QB & " make: "
  touchdowns = Wscript.StdIn.ReadLine
  if IsNumeric(touchdowns) then
    touchdowns = CInt(touchdowns)
  else
    Wscript.StdOut.Write "You did not enter a number. Please try again."
  end if
loop while IsNumeric(touchdowns) = false

'interceptions
do
  Wscript.StdOut.WriteLine "How many interceptions did " & QB & " throw: "
  interceptions = Wscript.StdIn.ReadLine
  if IsNumeric(interceptions) then
    interceptions = CInt(interceptions)
  else
    Wscript.StdOut.Write "You did not enter a number. Please try again."
  end if
loop while IsNumeric(interceptions) = false

'Passer rating formulae

'Percentage of completions formula
formA = (((completions / attempts) * 100) - 30) *.05
if formA < 0 then
  formA = 0
else
  if formA > 2.375 then 
    formA = 2.375
  else
    formA = FormatNumber(formA, 3)
  end if
end if

'Average yards gained per attempts formula  
formB = ((yards / attempts) - 3) * .25 
if formB < 0 then
  formB = 0
else
  if formB > 2.375 then 
    formB = 2.375
  else
    formB = FormatNumber(formB, 3)
  end if
end if

'Percentage of touchdowns formula
formC = (touchdowns / attempts) * 20
if formC > 2.375 then
  formC = 2.375
else
  formC = FormatNumber(formC, 3)
end if

'Percentage of interceptions formula
formD = 2.375 - ((interceptions / attempts) * 25)
if formD < 0 then   
  formD = 0
else
  formD = FormatNumber(formD, 3)
end if

'Summation formula
passerRating = ((formA + formB + formC + formD) / 6) * 100

Wscript.StdOut.WriteLine QB & " has a passer rating of " & FormatNumber(passerRating, 1)

我相信数学逻辑是真实和正确的,我也相信我所有的数据类型转换都是准确的。现在我将为您提供来自他们的website 的 NFL 球员的四分卫评分公式。

例如,以史蒂夫·杨 1994 年的创纪录赛季为例,当时他完成了 461 次传球中的 324 次,推进 3,969 码、35 次达阵和 10 次拦截。

四个计算将是:

  • 完成百分比 — 461 中的 324 是 70.28%。减去 30 从完成百分比 (40.28) 并将结果乘以 0.05。结果是 2.014 分。注意:如果结果小于零(Comp. Pct. 小于 30.0),则奖励零分。如果 结果大于 2.375(Comp. Pct. 大于 77.5),奖励 2.375。
  • 每次尝试获得的平均码数 - 3,969 码除以 461 尝试次数为 8.61。从每次尝试的码数中减去三码 (5.61) 并将结果乘以 0.25。结果是 1.403。注意:如果 结果小于零(每次尝试的码数小于 3.0),奖励 零分。如果结果大于 2.375(每次尝试码数 大于 12.5),奖励 2.375 分。
  • 达阵传球百分比 — 461 次尝试达阵 35 次 7.59%。将达阵百分比乘以 0.2。结果是 1.518。注意:如果结果大于 2.375(达阵百分比大于 11.875),则奖励 2.375。
  • 拦截百分比 — 461 次尝试中的 10 次拦截是 2.17%。将拦截百分比乘以 0.25 (0.542) 并从 2.375 中减去该数字。结果是 1.833。注意:如果 结果小于零(拦截百分比大于 9.5),奖励零分。
  • 四步之和为 (2.014 + 1.403 + 1.518 + 1.833) 6.768。 然后将总和除以 6 (1.128) 并乘以 100。在此 情况下,结果为 112.8。相同的公式可用于确定 任何尝试至少一次传球的球员的传球评分。

我已经测试过这个问题并将其隔离为:

如果我输入:

Attempt: 469
Completions: 281
Yards: 1406
Touchdowns: 17
Interceptions: 15

但是,如果我输入这个:

Attempts: 469
Completions: 281
Yards: 1407
Touchdowns: 17
Interceptions: 15

我收到一条错误消息:

运行时错误 - 类型不匹配 'String""

出现的行光标错误在passerRating变量行上。

有人知道我应该如何解决这个问题吗?

【问题讨论】:

  • 我相信formatNumber() 函数返回一个字符串。所以你试图将 4 个字符串加在一起,但它失败了。 msdn.microsoft.com/en-us/library/xfta99yt(v=vs.90).aspx
  • 它适用于我发布的第一组数字,但不适用于第二组。如果 FormatNumber 是问题,我输入的任何数字都不会失败吗?
  • 没有。因为你在那里有一个if 声明。当passes = 1407 你从你的公式中得到一个0 所以它不会去formatNumber()。当passes = 1406 然后公式吐出-0.00053 并且你最终点击formatNumber() 并导致最后的加法失败,因为你不能一起添加一个字符串和一个数字。相反,只需传递结果并在所有算术完成后最后执行formatNumber()
  • 尝试Wscript.StdOut.WriteLine formA &amp; "|" &amp; formB &amp; "|" &amp; formC &amp; "|" &amp; formD 之前 passerRating 变量行。我想例如formA = FormatNumber(formA, 3) 给出一个非数字字符串而不是 "2.375",例如"2,375"" 2.375"(注意前导空格)等...使用 Round() 代替 FormatNumber(),如下所示:formA = Round(formA, 3)
  • 或使用passerRating = ((0 + formA + formB + formC + formD) / 6) * 100 强制将behavior of the + operator 作为算术和(自动转换为数字变量)而不是字符串连接。

标签: vbscript type-mismatch


【解决方案1】:

如果您想将该数字用于进一步计算,请勿使用FormatNumber。该函数的目的是生成数字的格式化字符串表示形式以供输出。删除 else 分支:

if ... then 
  formX = 2.375
else
  formX = FormatNumber(formX, 3)
end if

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-01-04
    • 1970-01-01
    • 2022-06-29
    • 2016-08-27
    • 2013-01-16
    • 1970-01-01
    • 2018-01-31
    相关资源
    最近更新 更多