i need some help. i tried a lot of time using loops but i cannot figure
it out wha i want.
is basicaly this:
i type a letter and i have the output in hiragana but the program can´t
finish untill i press ‘q’ and if type another letter it joins to the
first…
this is my code to start…
class Test
def initialize
vowels = {‘a’=>‘あ’, ‘i’=>‘い’, ‘u’=>‘う’, ‘e’=>‘え’, ‘o’=>‘お’}
letter = gets.chomp.downcase
# logic here
end
end
t = Test.new
this way i can enter a letter and get a hirana equivalent
but when it happens the program stops!
class Test
def initialize
vowels = Hash[‘a’=>‘あ’, ‘i’=>‘い’, ‘u’=>‘う’, ‘e’=>‘え’, ‘o’=>‘お’]
letter = gets.chomp.downcase
# logic here
case letter
when ‘a’
print “#{vowels[‘a’]}”
when ‘i’
print “#{vowels[‘i’]}”
when ‘u’
print “#{vowels[‘u’]}”
when ‘e’
print “#{vowels[‘e’]}”
when ‘o’
print “#{vowels[‘o’]}”
else
puts “This letter is not part of hiragana alphabet!”
end
end
end
t = Test.new
i found almost what i was looking for:
class Test2
def initialize
vowels = Hash[‘a’=>‘あ’, ‘i’=>‘い’, ‘u’=>‘う’, ‘e’=>‘え’, ‘o’=>‘お’]
letter = gets.chomp.downcase
while letter != 'q'
if letter == 'a'
print "#{vowels['a']}"
letter = gets.chomp.downcase
elsif letter == 'i'
print "#{vowels['i']}"
letter = gets.chomp.downcase
elsif letter == 'u'
print "#{vowels['u']}"
letter = gets.chomp.downcase
elsif letter == 'e'
print "#{vowels['e']}"
letter = gets.chomp.downcase
elsif letter == 'o'
print "#{vowels['o']}"
letter = gets.chomp.downcase
else
puts "This letter is not part of hiragana alphabet!"
break
end
end
end
end
t2 = Test2.new
def ask() print(" ? :"); gets.chomp.downcase end
vowels = Hash[‘a’=>‘あ’, ‘i’=>‘い’, ‘u’=>‘う’, ‘e’=>‘え’, ‘o’=>‘お’]
letter=ask()
while letter != ‘q’
puts jap=vowels[letter] ? “Ok: #{jap}” : “Nok!”
letter = ask()
end
i don’t know if this is the best code but here what i have done:
true
vowels = {‘a’=>‘あ’, ‘i’=>‘い’, ‘u’=>‘う’, ‘e’=>‘え’, ‘o’=>‘お’}
key = nil
while(key != “q”) do
key = gets.chomp
case key
when ‘a’
print “#{vowels[‘a’]}”
when ‘i’
print “#{vowels[‘i’]}”
when ‘u’
print “#{vowels[‘u’]}”
when ‘e’
print “#{vowels[‘e’]}”
when ‘o’
print “#{vowels[‘o’]}”
when ‘q’
puts ‘bye bye’
when ‘’
puts ‘No letter was typed!’
else
puts “This letter is not part of hiragana alphabet!”
end
end
this is the output i got. not the way i expected but a way to go close
of it.
you example is awesome more good than mine. but don’t work as i posted a
jpg in the previous reply.
Cristiano D. wrote in post #1172174:
you example is awesome more good than mine. but don’t work as i posted a
jpg in the previous reply.
I use ruby 2.0
It’s seem that ruby 2.1 need parentheses around the condition
try that :
def ask() print(“あなたの入力母音 :”); gets.chomp.downcase end
vowels = Hash[‘a’=>‘あ’, ‘i’=>‘い’, ‘u’=>‘う’, ‘e’=>‘え’, ‘o’=>‘お’]
letter=ask()
while letter != ‘q’
puts (jap=vowels[letter]) ? “それは良いです: #{jap}” : “これは適切ではありません!”
letter = ask()
end