我收到类型不匹配错误

时间:2015-10-21 20:55:40

标签: vbscript type-mismatch

我正在开展一个项目,以提高我的逻辑技能,计算四分卫的传球手评分。我已经尝试了调试这个问题的所有技能,但我仍然处于亏损状态。首先,我将向您展示我的代码。

'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(比较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.在此 case,结果是112.8。可以使用相同的公式来确定 任何尝试至少一次传球的球员的传球评分。

我已经测试了这个问题并将其分离到了这个问题:

如果我输入:

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

但是如果我输入这个:

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

我收到一条错误消息:

  

运行时错误 - 类型不匹配&#39;字符串&#34;&#34;

出现的行光标错误位于passerRating变量行。

有没有人知道我应该怎么做才能解决这个问题?

1 个答案:

答案 0 :(得分:1)

如果您想使用该号码进行进一步计算,请不要使用FormatNumber。该函数的目的是生成输出数字的格式化字符串表示。删除else分支:

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