# 新手问题 请问这是哪里出错了？

chankaward · 2012年10月25日 · 最后由 chankaward 回复于 2012年10月25日 · 3185 次阅读
``````puts "=>Hello, please enter player1 name."
name1 = gets.chomp
puts "=>Hello, please enter player2 name."
name2 = gets.chomp
puts "=>hello #{name1} and #{name2}, let's play the black jack game."

def random_cards
first_card = rand(10) + 1
second_card = rand(10) + 1
total_card = first_card + second_card
{first_card: first_card, second_card: second_card, total_card: total_card}
end

player1_cards = random_cards
player1_first_card, player1_second_card, player1_total_card = player1_cards[:first_card], player1_cards[:second_card], player1_cards[:total_card]

puts "=>Player One's first card is: #{player1_first_card}, second card is #{player1_second_card}, total is #{player1_total_card}."

player2_cards = random_cards
player2_first_card, player2_second_card, player2_total_card = player2_cards[:first_card], player2_cards[:second_card], player2_cards[:total_card]

puts "=>Player Two's first card is: #{player2_first_card}, second card is #{player2_second_card}, total is #{player2_total_card}."

if player1_total_card < player2_total_card
puts "=> Player Two won!!"
elsif player1_total_card > player2_total_card
puts "=> Player One won!!"
else
puts "=> This is a tie game!"
end

``````

Terminal

``````black_jack.rb: 1: syntax error, unexpected tIDENTIFIER, expecting \$end
puts "=>Hello, ...er1 name."
``````

``````puts "=>Hello, please enter player1 name."
name1 = gets.chomp
puts "=>Hello, please enter player2 name."
name2 = gets.chomp
puts "=>hello #{name1} and #{name2}, let's play the black jack game."

@first_card,@second_card,@total_card = nil
def random_cards_total
@first_card = rand(10) + 1
@second_card = rand(10) + 1
@total_card = @first_card + @second_card
{@first_card.to_s => @first_card, @second_card.to_s => @second_card, @total_card.to_s => @total_card}
end

player1_card = random_cards_total
player1_first_card, player1_second_card, player1_total_card = player1_card[@first_card.to_s], player1_card[@second_card.to_s], player1_card[@total_card.to_s]

puts "=>Player One's first card is: #{player1_first_card}, second card is #{player1_second_card}, total is #{player1_total_card}."

player2_card = random_cards_total
player2_first_card, player2_second_card, player2_total_card = player2_card[@first_card.to_s], player2_card[@second_card.to_s], player2_card[@total_card.to_s]

puts "=>Player Two's first card is: #{player2_first_card}, second card is #{player2_second_card}, total is #{player2_total_card}."

if(player2_total_card > player1_total_card)
puts "=> Player Two won!!"
elsif player1_total_card > player2_total_card
puts "=> Player One won!!"
else
puts "=> This is a tie game!"
end
``````

#1 楼 @help5305fff ```black_jack.rb: 1: syntax error, unexpected tIDENTIFIER, expecting \$end puts "=>Hello, ...er1 name."``` 请问这是哪里出错了？

#3 楼 @help5305fff 我找不到哪里少了 end 阿。。

#5 楼 @help5305fff 没有，不过我想知道我原来的哪里少了 end。

#8 楼 @yangyanhao 我改了那两处还是不行。。。还是老样子

player1_cards -> player1_card player2_cards -> player2_card

=>hello and , let's play the black jack game. =>Player One's first card is: 5, second card is 4, total is 9. =>Player Two's first card is: 2, second card is 10, total is 12. => Player Two won!!

#13 楼 @help5305fff 果然还是错误

#16 楼 @help5305fff UTF-8 。。。。。。

#16 楼 @help5305fff 还没。。是不是要改成 ASCII？

#18 楼 @help5305fff 我复制了代码，另外新建了一个文件可以运行了。请问这个问题应该怎样解决啊?原来的代码我是用个 iphone 上的软件写的。 #13 楼 @help5305fff 受教了。;-) 谢谢

。。。。那就是编码问题了

#21 楼 @help5305fff 应该改成什么呢？