2015-10-10 22 views
-1

錯誤也許我沒有看到,但我真的相信,我有一個做永遠循環!有人可以幫忙嗎?我從頭開始做,這樣我就可以讓用戶再次使用代碼。感謝任何幫助人。我有說我有一個循環沒有做

代碼:

'Written by Douglas Howe 
Option Explicit 
Dim colorPicOne, colorPicTwo, tryAgain 

'initial do 
do 
    'first prompt 
    do 
    Wscript.StdOut.WriteLine "Pick a primary color (i.e blue, red, or yellow): " 
    colorPicOne = lcase(wscript.StdIn.ReadLine) 
    if colorPicOne <> "blue" and colorPicOne <> "red" and colorPicOne <> "yellow" then 
     Wscript.StdOut.WriteLine "Not a primary color! Please pick another color: " 
    else 
     exit do 
    end if 
    loop while colorPicOne <> "blue" OR colorPicOne <> "red" or colorPicOne <> "yellow" 

    'second prompt 
    do 
    Wscript.StdOut.WriteLine "Pick another primary color (i.e blue, red, or yellow): " 
    colorPicTwo = lcase(wscript.StdIn.ReadLine) 
    if colorPicTwo <> "blue" and colorPicTwo <> "red" and colorPicTwo <> "yellow" then 
     Wscript.StdOut.WriteLine "Not a primary color! Please pick another color: " 
    else 
     exit do 
    end if 
    loop while colorPicTwo <> "blue" OR colorPicTwo <> "red" or colorPicTwo <> "yellow" 

    if colorPicOne = "red" and colorPicTwo = "blue" then 
    Wscript.StdOut.Writeline "Your color is purple!" 
    else 
    if colorPicOne = "blue" and colorPicTwo ="red" then 
     Wscript.StdOut.Writeline "Your color is purple!" 
    end if 


    if colorPicOne = "red" and colorPicTwo = "yellow" then 
    Wscript.StdOut.Writeline "Your color is orange!" 
    else 
    if colorPicOne = "yellow" and colorPicTwo ="red" then 
     Wscript.StdOut.Writeline "Your color is orange!" 
    end if 

    if colorPicOne = "blue" and colorPicTwo = "yellow" then 
    Wscript.StdOut.Writeline "Your color is green!" 
    else 
    if colorPicOne = "yellow" and colorPicTwo ="blue" then 
     Wscript.StdOut.Writeline "Your color is green!" 
    end if 

    Wscript.Stdout.WriteLine "Would you like to try again? (yes or no) :" 
    tryAgain =lcase(Wscript.StdIn.ReadLine) 

    if tryAgain = "no" then 
    Wscript.Stdout.WriteLine "Have a nice day" 
    exit do 
    if tryAgain = "yes" then 
     Wscript.Stdout.WriteLine "Ok!" 
    else 
     Wscript.Stdout.WriteLine "Sorry, you gave an invalid response. Good Bye!" 
     exit do 
    end if 
    end if 
loop while tryAgain = "yes" 

,我有一個loop沒有do我的錯誤狀態。

+0

也許你可以縮短你的代碼更一致。這將會更容易解決這些錯誤。 –

+0

非常感謝。我會爲此努力。我仍然是一個小白菜,所以我感謝耐心和回答。 –

回答

1

問題不是你有一個沒有做的循環,而是你有幾個(我發現3)塊if聲明沒有正確關閉end if。在所產生的嵌套的水平最終loop沒有一個do。我認爲這是你的意圖:

'Written by Douglas Howe 
Option Explicit 
Dim colorPicOne, colorPicTwo, tryAgain 

'initial do 
do 
    'first prompt 
    do 
     Wscript.StdOut.WriteLine "Pick a primary color (i.e blue, red, or yellow): " 
     colorPicOne = lcase(wscript.StdIn.ReadLine) 
     if colorPicOne <> "blue" and colorPicOne <> "red" and colorPicOne <> "yellow" then 
      Wscript.StdOut.WriteLine "Not a primary color! Please pick another color: " 
     else 
      exit do 
     end if 
    loop while colorPicOne <> "blue" OR colorPicOne <> "red" or colorPicOne <> "yellow" 

    'second prompt 
    do 
     Wscript.StdOut.WriteLine "Pick another primary color (i.e blue, red, or yellow): " 
     colorPicTwo = lcase(wscript.StdIn.ReadLine) 
     if colorPicTwo <> "blue" and colorPicTwo <> "red" and colorPicTwo <> "yellow" then 
      Wscript.StdOut.WriteLine "Not a primary color! Please pick another color: " 
     else 
      exit do 
     end if 
    loop while colorPicTwo <> "blue" OR colorPicTwo <> "red" or colorPicTwo <> "yellow" 

    if colorPicOne = "red" and colorPicTwo = "blue" then 
     Wscript.StdOut.Writeline "Your color is purple!" 
    else 
     if colorPicOne = "blue" and colorPicTwo ="red" then 
      Wscript.StdOut.Writeline "Your color is purple!" 
     end if 'This was missing 
    end if 

    if colorPicOne = "red" and colorPicTwo = "yellow" then 
     Wscript.StdOut.Writeline "Your color is orange!" 
    else 
     if colorPicOne = "yellow" and colorPicTwo ="red" then 
      Wscript.StdOut.Writeline "Your color is orange!" 
     end if 

     if colorPicOne = "blue" and colorPicTwo = "yellow" then 
      Wscript.StdOut.Writeline "Your color is green!" 
     else 
      if colorPicOne = "yellow" and colorPicTwo ="blue" then 
       Wscript.StdOut.Writeline "Your color is green!" 
      end if 'This was missing 
     end if 

     Wscript.Stdout.WriteLine "Would you like to try again? (yes or no) :" 
     tryAgain =lcase(Wscript.StdIn.ReadLine) 

     if tryAgain = "no" then 
      Wscript.Stdout.WriteLine "Have a nice day" 
      exit do 
      if tryAgain = "yes" then 
       Wscript.Stdout.WriteLine "Ok!" 
      else 
       Wscript.Stdout.WriteLine "Sorry, you gave an invalid response. Good Bye!" 
       exit do 
      end if 'This was missing 
     end if 
    end if 
loop while tryAgain = "yes" 
+0

感謝您的幫助。最後的「失蹤」結束,如果是不必要的。該代碼拋出了一個錯誤 –

0

該錯誤消息是誤導性的。問題不在於你有Loop沒有Do,但你必須在它並沒有在同一背景下,前Do一個地方Loop

這是你所觀察到的行爲的實際原因:

if colorPicOne = "red" and colorPicTwo = "blue" then 
    Wscript.StdOut.Writeline "Your color is purple!" 
else 
    if colorPicOne = "blue" and colorPicTwo ="red" then 
    Wscript.StdOut.Writeline "Your color is purple!" 
end if 

什麼你大概的意思是,從縮進來看,是這樣的:

if colorPicOne = "red" and colorPicTwo = "blue" then 
    Wscript.StdOut.Writeline "Your color is purple!" 
else 
    if colorPicOne = "blue" and colorPicTwo ="red" then Wscript.StdOut.Writeline "Your color is purple!" 
end if 

但你確實有(用適當的縮進)是這樣的:

if colorPicOne = "red" and colorPicTwo = "blue" then 
    Wscript.StdOut.Writeline "Your color is purple!" 
else 
    if colorPicOne = "blue" and colorPicTwo ="red" then 
    Wscript.StdOut.Writeline "Your color is purple!" 
    end if 

             
  
    end if 
   '<-- missing "end if" here!

基本上,你嵌套在一個If..End If所述Else分支另一If表達的,但不關閉該Else分支。正因爲如此也嵌套分支內的所有後續語句,以便第一Do最後Loop在不同的上下文。

在VBScript中你可以寫If表達要麼是這樣的:

If condition Then statement Else other_statement 

或像這樣:

If condition Then 
    statement 
Else 
    other_statement 
End If 

也就是說,如果你把statement在新行你必須關閉整個表達式與End If。唯一的例外是包裝線,續行符(_):

If condition Then _ 
    statement 

以上是有效的,而不End If,因爲它在技術上只是一個單一的線。

+0

感謝您的幫助 –