2016-12-05 79 views
1

我必須創建一個程序,我必須在一行中詢問用戶的姓名。如果用戶只放置他們的第一個OR姓,它將通過該例外被拒絕。我在最後(如下)不斷收到錯誤。Ruby異常初始化錯誤

class MyNewException < Exception 
     attr_accessor :first, :last 
     def initialize (first, last) 
     @first = first 
     @last = last 
     end 
    end 

print "Enter your first and last name:" 

begin 

first, last = gets.chomp.split 

print "Hello," + first + " " + last + "!" 

if last.size == 0 
    raise MyNewException, "Sorry, I didn't catch that! Try again:" 
end 
rescue MyNewException 
    puts "Sorry, I didn't catch that. Try again:" 
    retry 
end 

不斷收到一個錯誤:

testing.rb:15:in `+': no implicit conversion of nil into String (TypeError) 
+0

添加一個默認值'高清初始化(第一=零,最後=零) ' – Beginner

+0

這裏有兩個錯誤:一個是缺少逗號('raise MyNewException,「對不起,我沒有完全理解它!再試一次:'')或者顯式構造一個異常('raise MyNewException.new(」Sorry,我不太明白,再試一次:「)');另一個是你的例外錯誤的構造函數。你的例外爲什麼有一個名字? :)異常通常只在其默認構造函數中有一條消息。如果你真的想用一個名字來定製一個異常,你可以用'raise MyNewException.new('John','Doe')'來提高它。 – Amadan

+0

@NewbeeDev謝謝你的工作!另外,爲了重新詢問用戶全名,我是否在救援中創建了一個循環? –

回答

1

raise() docs有故障。你可以這樣做:

puts "Enter your first and last name:" 
name = gets.chomp 

class MyNewException < Exception 

    def initialize(str) 
    super(str) #pass the value for the message property to the parent class 
    end 

end 

begin 
    raise MyNewException, "Sorry, I didn't quite catch that! Try again:" 
rescue MyNewException => e 
    puts e.message 
end 

--output:-- 
Enter your first and last name: 
Sarah Kim 
Sorry, I didn't quite catch that! Try again: 

或者:

puts "Enter your first and last name:" 
name = gets.chomp 

class MyNewException < Exception 

    attr_accessor :first, :last 

    def initialize(user_name, exception_message) 
    @first, @last = user_name.split 
    super exception_message 
    end 

end 

begin 
    raise MyNewException.new(name, "I didn't quite catch that! Try again:") 
rescue MyNewException => e 
    puts e.message 
    puts e.first 
    puts e.last 
end 

--output:-- 
Enter your first and last name: 
Sarah Kim 
I didn't quite catch that! Try again: 
Sarah 
Kim 

下面是如何寫一個無限循環獲取用戶輸入:

class Person 
    attr_accessor :first, :last 

    def initialize(first, last) 
    @first = first.capitalize 
    @last = last.capitalize 
    end 
end 

while true 
    puts "Enter your first and last name:" 
    first, last = gets.chomp.split 
    #Show what you got: 
    p first 
    p last 

    if first and last 
    #If no names were entered, both first and last will be nil. 
    #If one name was entered, first will evaluate to true and last will be nil. 
    #If two names were entered, first and last will evaluate to true. 
    #If two or more names were entered, first and last will evaluate to true. 
    user = Person.new first, last 
    break #jump to the line immediately after the infinite loop 
    end 

    puts "Sorry, you must enter both a first and a last name. Try again" 
end 

puts "Thanks #{user.first} #{user.last}!" 
+0

感謝您的兩個建議!現在我很困惑在哪裏放置循環來重新請求重新輸入名稱......我是否再次爲循環創建一個類? @ 7stud –

+0

不,班級應該走在最前面 - 在循環之外。通常情況下,當你想要求某人輸入時,你會創建一個無限循環,不斷地詢問輸入,如果輸入正確,你就會跳出循環,這將防止再次提出問題。你不需要一個例外來做到這一點。 – 7stud

+0

在我看來,你正在嘗試混合一個Exception對象和一個Person對象。目前還不清楚爲什麼要這麼做,即爲什麼應該使用Exception對象來存儲用戶輸入? – 7stud

1

創建一個循環,做你的試驗和錯誤。 作爲我的回答,我選擇while循環

i = 0 
while i < 1 do 

    if condition == true 
     # display your greetings then increment loop to end 
     i += 1 
    else 
     # display error message 
    end 

end 

所以,你的代碼會是這樣

class MyNewException < Exception 

    attr_accessor :first, :last, :valid 

    def initialize(first, last) 
    @first = first 
    @last = last 
    @valid = true 
    if first.empty? || last.empty? then 
     @valid = false 
    end 
    end 

end 

i = 0 

while i < 1 do 

    print "Enter your first name: " 
    firstname = gets.chomp 
    print "Enter your last name: " 
    lastname = gets.chomp 

    name = MyNewException.new(firstname, lastname) 

    if name.valid then 
     puts "Greetings " << name.first << " " << name.last 
     i += 1 
    else 
     puts "I didn't quite catch that! Try again:" 
    end 

end 

enter image description here

+0

誰投下了這個請解釋爲什麼! – Beginner

+0

不知道爲什麼有人下了這個大聲笑!你真是太棒了!我掙扎的主要原因是這個程序的輸出應該是同時給出的名字和姓氏。所以用戶應該能夠在整個開始時輸入他們的全名,而不是「輸入你的名字,輸入你的姓氏」 –

+0

@SarahKim也許標記它的答案和upvote讓我滿意,在我的努力工作後,在這段代碼中: ) – Beginner