2014-03-25 106 views
0
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」 所以我知道爲什麼我收到這個錯誤我只是無法弄清楚這到底是造成它

我需要新鮮的眼睛指向正確的方向!

+4

,你需要首先什麼是正確的縮進。 –

+0

Bad indentation is BAD m'kay?大多數缺失問題都是由於你個人無法追蹤開始和結束......如果你縮進 - 它總是變得更加清晰(只要將你的眼睛放在代碼中,直到你找到一個沒有縮進的縮進結束,你已經知道了) –

+0

我的答案顯示了你的代碼的正確縮進,因此很容易發現缺失的元素。如果你使用的是類似vim或者一個好的編輯器,它通常會有一個清理/修復縮進功能。在vim中,你可以突出顯示你的代碼(shift + v,然後導航到代碼結束)並按下equals。有很多捷徑可以做到這一點 –

回答

0

您在評估中發現end之一導致問題時錯誤。相反,你需要更多的end才能使一切平衡。縮進代碼(或使用編寫代碼的文本編輯器)會使這一點變得很明顯。

添加一個endprintrecord,另一seaverecord前,兩個的,在更多的主循環似乎之前是你的意思是什麼,在工作計劃的結果。

2

你缺少def printrecord()end之前,saverecord()之前end然後,在最後end上最後一行。請縮進你的代碼!

1

對於大型Classes和嵌套式條件語句,正確的縮進是沿用的方式。我相信你的問題在deleterecord方法。您關閉了條件語句 ,但需要另一個end來關閉該方法。這同樣適用於在printmethod!

def deleterecord 
    if 
    code 
    else 
    code 
    end 
NEED ANOTHER end HERE 
0

你的代碼需要相當多的縮進和間距的幫助。如果你解決,它更容易找到缺失的元素

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 

缺少結束

0

你錯過三次結束。您100%的工作代碼

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 
    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 

    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 
end 

以供將來參考始終使用NetBeans 6.9.1這種類型的錯誤

相關問題