class CSUN
def initialize()
# create hashes
@lastnamehash = Hash.new()
@firstname = Hash.new()
@major = Hash.new()
@catalogyear = Hash.new()
puts "database created"
end
# Add Record
def addrecord()
#Prompt for LastName, FrstName, Major, Catalog Year
print "Enter Student's Last name: "
lastname = gets.chomp
print "Enter Student's First Name: "
firstname = gets.chomp
Print "Enter Student's ID: "
id = gets.chomp
Print "Enter Student's Major: "
major = gets.chomp
Print "Enter Catalog Year: "
catalogyear = gets.chomp
if (@lastnamehash [ID])
puts "Student Already Exists!"
return
end
#store in hash
@lastnamehash[ID] = lastname
@firstnamehash[ID] = firstname
@majorhash[ID] = major
@catalogyearhash[ID] = catalogyear
end
#update record
def updaterecord()
#Prompt for record by ID
print "Enter Student's ID: "
id=gets.chomp
if !(@lastnameash [ID])
puts "Record does not exist"
return
else
puts "Update Student's Last Name"
puts "current value: #{@lastnamehash [ID]}"
print "New Value: "
lastname = gets.chomp
@lastnamehash[ID] = lastname
puts "Update Student's First Name"
puts "current value: #{@firstnamehash [ID]}"
print "New Value: "
firstname = gets.chomp
@firstnamehash[ID] = firstname
puts "Update Student's major"
puts "current value: #{@majorhash [ID]}"
print "New Value: "
major = gets.chomp
@majorhash[ID] = major
puts "Update Student's catalog year"
puts "current value: #{@catalogyearhash [ID]}"
print "New Value: "
catalogyear = gets.chomp
@catalogyearhash[ID] = catalogyear
end
end
#delete record
def deleterecord()
if !(@lastnameash [ID])
puts "Record does not exist"
return
else
@lastnamehash.delete(ID)
@firstnamehash.delete(ID)
@majorhash.delete(ID)
@catalogyear.delete(ID)
puts"Record Deleted"
end
#printrecord()
def printrecord()
@lastnamehash.each do |key,value|
puts "Student ID: #{key}"
puts "Last name: #{value}"
puts "First name: #{@firstnamehash[key]}"
puts "Major: #{@majorhash[key]}"
puts "Catalog Year: #{@catalogyearhash[key]}"
end
#save record
def saverecord()
print "save record (y/n): "
save1 = gets.chomp
if save1!= "y"
return
else
savefile = File.new("records.dat","w")
contents = " "
@lastnamehash.each do |key,value|
contents = contents + key + values + @firstnamehash[key] + majorhash[key] + catalogyearhash
end
savefiles.syswrite (contents)
savefiles.close()
puts"contents saved"
end
#main loop
choice = 0
while (choice !=6)
#propmt for choice
choice = gets.chomp
case(choice)
when"1";
db = CSUN.new
when"2"
db.addrecord
when"3"
db.updaterecord
when"4"
db.deleterecord
when"5"
db.printrecord
when"6"
db.saverecord
end
end
end
我不斷收到此錯誤「意外$ ND期待關鍵字end」 所以我知道爲什麼我收到這個錯誤我只是無法弄清楚這到底是造成它
我需要新鮮的眼睛指向正確的方向!
,你需要首先什麼是正確的縮進。 –
Bad indentation is BAD m'kay?大多數缺失問題都是由於你個人無法追蹤開始和結束......如果你縮進 - 它總是變得更加清晰(只要將你的眼睛放在代碼中,直到你找到一個沒有縮進的縮進結束,你已經知道了) –
我的答案顯示了你的代碼的正確縮進,因此很容易發現缺失的元素。如果你使用的是類似vim或者一個好的編輯器,它通常會有一個清理/修復縮進功能。在vim中,你可以突出顯示你的代碼(shift + v,然後導航到代碼結束)並按下equals。有很多捷徑可以做到這一點 –