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期待关键字结束" 所以我知道为什么我得到这个错误我只是想知道哪一端导致它
我需要新鲜的眼睛指向正确的方向!
答案 0 :(得分:2)
您在end
之前错过了def printrecord( )
,在end
之前错过了saverecord()
,在最后一行错过了end
。请缩进你的代码!
答案 1 :(得分:1)
适当缩进适用于大型类和嵌套条件语句。我相信deleterecord
方法的问题。您关闭了条件语句
但需要另一个end
来关闭该方法。 printmethod!
def deleterecord
if
code
else
code
end
NEED ANOTHER end HERE
答案 2 :(得分:0)
您的评估中有一个end
导致了问题。相反,你还需要几个end
来使一切平衡。缩进代码(或使用为编写代码而编写的文本编辑器)将使这一点显而易见。
在end
之前添加printrecord
,在seaverecord
之前添加另一个{{1}},在主循环之前添加两个似乎是你的意思并产生一个有效的程序。
答案 3 :(得分: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
MISSING 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
MISSING END
答案 4 :(得分: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此类错误