2016-09-23 65 views
-3
  1. 幫助我這個代碼T_T爲什麼它繼續顯示語法錯誤,意外「:」 exepcting結束輸入數據庫:{語法錯誤,意外「:」希望結束輸入

    database: { 
        total_car_price: 20000, 
        stock_car_price: 20000, 
        features: { 
         rim: { 
          '16' => 50, 
          '15' => 30, 
          '14' => 10 
         }, 
         color: { 
          'blue' => 0, 
          'red' => 0, 
          'yellow' => 0 
         }, 
         tint: { 
          '100' => 80, 
          '80' => 50, 
          '50' => 0 
         }, 
         seat: { 
          'leather' => 500, 
          'PVC' => 300 
         } 
        } 
    } 
    
    puts "Original price : #{database[:stock_car_price]}" 
    
    database[:features].each do |feature, data| 
        puts feature.upcase 
        data.each do |option, extra_cost| 
         puts "#{option} :: #{extra_cost}" 
        end 
        while true 
         selection = gets.chomp 
         if data.keys.include? selection #make it general that can accept string/integer 
          database[:total_car_price] += data[selection] 
          break 
         else 
          puts 'Incorrect selection!' 
         end 
        end 
    end 
    
    
    puts "Stock Price :: #{database[:stock_car_price]}" 
    puts "Final Price :: #{database[:total_car_price]}" 
    
+0

請正確格式化您的代碼。 –

+0

嘗試'ruby -w '以獲得有關錯誤發生位置的提示。 –

+0

你試過我的答案嗎? – araratan

回答

1

你應該分析你JSON是這樣的:

json = { 
    database: { 
    total_car_price: 20000, 
    stock_car_price: 20000, 
    features: { 
     rim: { 
      '16' => 50, 
      '15' => 30, 
      '14' => 10 
     }, 
     color: { 
      'blue' => 0, 
      'red' => 0, 
      'yellow' => 0 
     }, 
     tint: { 
      '100' => 80, 
      '80' => 50, 
      '50' => 0 
     }, 
     seat: { 
      'leather' => 500, 
      'PVC' => 300 
     } 
    } 
} 
} 

parsed_json = JSON.parse(json.to_json) 

puts "Original price : #{parsed_json['database']['stock_car_price']}" 

這裏是工作樣例:

working code

相關問題