2017-03-03 50 views
-1

我遇到了我的代碼問題,似乎無法弄清楚我需要改變什麼。這裏是我的三個文件,底部是我得到的錯誤。其他沒有救援是沒用的

require './PokerHand' 
    require "./Constants" 
    require 'minitest/autorun' 

    class TestClass < MiniTest::Test 
     include Constants 

     def test_1 
      arr1 = [Card.new(2, "S"), Card.new(3, "S"), 
      Card.new(4, "S"), Card.new(5, "S"), 
      Card.new(6, "S")] 
      ph1 = PokerHand.new(arr1) 
      ph1.classify 
      assert_equal STRAIGHT_FLUSH, ph1.hand_type 
     end 

     def test_2 
     arr2 = [Card.new(9, "C"), Card.new(9, "S"), 
      Card.new(9, "H"), Card.new(9, "D"), 
      Card.new(11, "S")] 
     ph2 = PokerHand.new(arr2) 
     ph2.classify 
     assert_equal FOUR_OF_A_KIND, ph2.hand_type 
     end 

     def test_3 
     arr3 = [Card.new(4, "C"), Card.new(9, "S"), 
      Card.new(9, "H"), Card.new(9, "D"), 
      Card.new(9, "C")] 
     ph3 = PokerHand.new(arr3) 
     ph3.classify 
     assert_equal FOUR_OF_A_KIND, ph3.hand_type 
     end 

新文件Pokerhand.rb:

require "./Constants" 
require "./Card" 
require "./Deck" 
require "./CardSpaceship" 

class PokerHand < Deck 
    include Constants 
    attr_reader :hand_type 

    def initialize(the_cards) 
    @cards = [ ] 
    @hand_type = UNCLASSIFIED 
    for card in the_cards 
    @cards << card 
    end 
    end 


# Determine hand type of PokerHand object. 
def classify 

    @cards.sort! 

    # Straight flush 
    if @cards[0].rank == @cards[1].rank +1 && 
    @cards[1].rank == @cards[2].rank +1 && 
    @cards[2].rank == @cards[3].rank +1 && 
    @cards[3].rank == @cards[4].rank +1 && 
    @cards[0].suit == @cards[1].suit && 
    @cards[1].suit == @cards[2].suit && 
    @cards[2].suit == @cards[3].suit && 
    @cards[3].suit == @cards[4].suit 
    @hand_type = STRAIGHT_FLUSH 
    end 
end 

新文件test2.rb:

require './PokerHand' 
require "./Constants" 
require 'minitest/autorun' 

class TestClass < MiniTest::Test 
    include Constants 

    def test_1 
     arr1 = [Card.new(2, "S"), Card.new(3, "S"), 
     Card.new(4, "S"), Card.new(5, "S"), 
     Card.new(6, "S")] 
     ph1 = PokerHand.new(arr1) 
     ph1.classify 
     assert_equal STRAIGHT_FLUSH, ph1.hand_type 
    end 

收到錯誤:

TestClass#test_1: 
PokerHand.rb:145: warning: else without rescue is useless 
C:/Ruby22/lib/ruby/2.2.0/rubygems/core_ext/kernel_require.rb:54:in `require': JulianHansen_P5/PokerHand.rb:30: syntax error, unexpected tINTEGER, expecting keyword_then or ';' or '\n' (SyntaxError) 
    if @cards[0].rank == @cards[1].rank +1 && 
             ^
PokerHand.rb:31: syntax error, unexpected tINTEGER, expecting keyword_end 
    @cards[1].rank == @cards[2].rank +1 && 
+0

你需要一個空間:'@cards [1] .rank + 1'錯誤也是語法錯誤,而不是「別無救援」(我看不到任何「其他」,但看看https://stackoverflow.com/questions/21817655/ruby-else-without-rescue-is -無用)。 – Ryan

回答

1

當你有warning: else without rescue is useless警告發生if/else語句設置不當LY。

例如: if true puts 'hi'' end # end shouldn't be here else puts 'whoops' end

你會發現,在你的代碼並糾正它爲好,雖然這不是什麼原因造成的致命錯誤。

+1在你的if語句要麼需要空格或括號

if @cards[0].rank == @cards[1].rank + 1 &&

if @cards[0].rank == (@cards[1].rank +1) &&