【发布时间】:2019-10-18 11:27:39
【问题描述】:
当它应该打印卡ID和余额时,它只会在屏幕上输出两个空白行
我已经完全重写了代码。 我已经摆弄了那个代码一个小时
class RBC
def initialize
@args = ["Create a new card"]
@functions = ["create_rbc"]
puts "Do you have an RBC ID yet? Yes(0) No(1)"
hasrbc = gets.chomp.to_i
if hasrbc == 1
@balance = 5
create_rbc
else
login
end
end
def create_rbc
puts "\nGenerating your rbc\n\n"
puts "\nWelcome to your Ruby Binary Card(RBC)!\n\n"
puts "Your RBC will keep track of your RubyCredits(RC).\n"
puts "You will get paid RC for work apps, and pay for game apps.\n"
puts "If you lose track of your RBC ID, you can get a new one.\n"
puts "Doing this, however, will reset your balance to the default of $5\n\n"
puts "What is your name? Do first last\n"
@fullname = gets.chomp
@card_name = get_name_codec(@fullname)
@card_cipher = "#{rand(1..9)}#{rand(1..9)}#{rand(1..9)}#{rand(1..9)}#{rand(1..9)}#{rand(1..9)}#{rand(1..9)}#{rand(1..9)}#{rand(1..9)}#{rand(1..9)}"
@card_id = "#{@card_name} - #{@card_cipher}"
instance_variable_set("@Id#{@card_cipher}", @balance)
puts "Write down your RBC ID: #{@card_id}"
file = File.open("Cards.rbc", "w")
file.puts @card_id
file.puts @balance
end
end
def get_name_codec(name)
names = name.split(" ")
fname = names[0]
lname = names[1]
fchar = fname.split(//)
fcodec = "#{fchar[0]}#{fchar[1]}"
name_codec = "#{fcodec}#{lname}"
return name_codec
end
def login
@found = false
puts "What is your RBC Id"
input = gets.chomp
File.open("Cards.RBC", "r") do |f|
f.each_line do |line|
if input == "#{line}"
@card_id = line.to_s
@found == true
elsif @found == true
@balance = line.to_i
end
end
end
puts "#{@card_id}#{@balance}"
end
RBC.new
然后是 Cards.RBC
TiLan - 1122632527
5
我希望它打印余额和卡 ID。 它应该给我我的卡 ID,然后是这样的余额: 0000...等 5
【问题讨论】: