我正在尝试编写一个程序,该程序接受用户的输入(句子),用户要旋转的单词,并围绕用户选择的单词输出旋转的句子。 例如。句子:这是一本书 旋转的单词:书 输出:book这是一个
我似乎无法退出输入数据的循环(程序不断要求输入,而不做其他任何事情。) 请帮忙。这是我的代码:
class SentenceRotator
def get_sentence
puts "Please enter your sentence: "
sentence = gets.chomp
get_word
end
def get_word
puts "Please enter the word you want to rotate around: "
word = gets.chomp
if converts_sentence_to_array.include?(word)
rotate_sentence_around_word
else
puts "Your word isn't in the sentence. Please enter another word."
word = gets.chomp
end
rotate_sentence_around_word
end
def converts_sentence_to_array()
get_sentence.split(" ")
end
def rotate_sentence_around_word()
new_array = converts_sentence_to_array.each_with_index {|word,index| converts_sentence_to_array.rotate(index)}
new_array
end
end
new_app = SentenceRotator.new
new_app.rotate_sentence_around_word
答案 0 :(得分:0)
这是您想要的逻辑,分为几行:
# input
sentence = 'This is a book'
word = 'book'
# processing
words = sentence.split
pos = words.index(word)
rotated = words.rotate(pos)
back_together = rotated.join(' ')
# output
p back_together
我建议您尽可能分离处理代码。然后,您可以专注于终端输入和输出逻辑,这实际上似乎是您的问题。
答案 1 :(得分:0)
我似乎无法退出这种输入数据的循环(程序不断要求输入,而不做其他任何事情。)
您似乎最主要的问题是正确接收用户的输入并输入旋转逻辑。您可以使用attr_reader
来访问跨方法的输入。我对您的班级进行了一些更改,以分多个步骤接受输入:
class SentenceRotator
attr_reader :sentence, :rotate_on_word
def get_sentence
puts "Please enter your sentence: "
@sentence = gets.chomp
end
def get_word_to_rotate_on
puts "Please enter the word you want to rotate around: "
@rotate_on_word = gets.chomp
unless sentence.include?(rotate_on_word)
puts "Your word isn't in the sentence. Please enter another word."
get_word_to_rotate_on
end
end
def rotate
puts sentence
puts rotate_on_word
puts 'You have all the info. Add the logic to rotate.'
end
end
> new_app = SentenceRotator.new
> new_app.get_sentence
Please enter your sentence:
This is a very funny book
> new_app
=> #<SentenceRotator:0x00007fee44178c40 @sentence="This is a very funny book">
> new_app.get_word_to_rotate_on
Please enter the word you want to rotate around:
a
> new_app
=> #<SentenceRotator:0x00007fee44178c40 @sentence="This is a very funny book", @rotate_on_word="a">
> new_app.rotate
This is a very funny book
a
You have all the info. Add the logic to rotate.
答案 2 :(得分:0)
new_app = SentenceRotator.new
new_app.rotate_sentence_around_word
因此,调用方法:rotate_sentence_around_word
=> converts_sentence_to_array
=> get_sentence
=> get_word
=> converts_sentence_to_array
=> get_sentence
=> ...
尝试这样的事情:
class SentenceRotator
def gets_user_data
puts "Please enter your sentence: "
@sentence = get_sentence.split(" ")
puts "Please enter the word you want to rotate around: "
@word = get_word_to_rotate_on
end
def get_sentence
gets.chomp
end
def get_word_to_rotate_on
word = gets.chomp
return word if @sentence.include?(word)
puts "Your word isn't in the sentence. Please enter another word."
get_word_to_rotate_on
end
def rotate_sentence_around_word()
gets_user_data
@sentence.rotate(@sentence.index(@word)).join(' ')
end
end
new_app = SentenceRotator.new
new_app.rotate_sentence_around_word