From: Glen Holcomb Date: 2010-04-13T06:28:49+09:00 Subject: Re: My application doesn't work well --000e0cd6e3246da61a048410d45f Content-Type: text/plain; charset=windows-1252 Content-Transfer-Encoding: quoted-printable On Mon, Apr 12, 2010 at 3:16 PM, Bruno Santana wrote: > Hi everybody, > > My application isn't working well. Let me try to explain what's wrong. > When I run my program appears something like: > > ruby pedrapapeltesoura.rb > Digite pedra, papel ou tesoura: pedra > 1 > tesoura > pedra > alguem ganhou > > This 1 that appears above is the value of resultSorteio variable, the > content of this variable come from a method which random a number > between 1 to 3. tesoura is the value of the variable maquina, however if > the variable resultSorteio is 1 the variable maquina should be pedra and > not be tesoura, and sometimes the variable is empty...if you try to play > many times you can see where is the word tesoura above will appears > nothing. Does somebody help me please. Here is my code: > > class Jogo > > def sorteia > opcoes =3D ['pedra','papel','tesoura'] > numero =3D 1 + rand(3) > return numero > end > > def joga(usuario) > > result =3D sorteia() > resultSorteio =3D result.to_i > > puts(resultSorteio) > puts (('11' * 2).to_i/2) > if resultSorteio =3D=3D 1 then > maquina =3D "pedra" > if resultSorteio =3D=3D 2 then > maquina =3D "papel" > else > maquina =3D "tesoura" > end > > end > > puts(maquina) > puts(usuario) > usuario.chop > if usuario =3D=3D maquina then > puts("empatou") > else > puts("alguem ganhou") > end > > end > end > > j =3D Jogo.new() > > print "Digite pedra, papel ou tesoura: " > opcaoUsuario =3D gets() > > j.joga(opcaoUsuario) > -- > Posted via http://www.ruby-forum.com/. > > Your if was nested funny, this should do what you want, I think: class Jogo def sorteia opcoes =3D ['pedra','papel','tesoura'] numero =3D 1 + rand(3) return numero end def joga(usuario) result =3D sorteia() resultSorteio =3D result.to_i puts(resultSorteio) puts (('11' * 2).to_i/2) if resultSorteio =3D=3D 1 then maquina =3D "pedra" elsif resultSorteio =3D=3D 2 then maquina =3D "papel" else maquina =3D "tesoura" end puts(maquina) puts(usuario) usuario.chop if usuario =3D=3D maquina then puts("empatou") else puts("alguem ganhou") end end end j =3D Jogo.new() print "Digite pedra, papel ou tesoura: " opcaoUsuario =3D gets() j.joga(opcaoUsuario) --=20 "Hey brother Christian with your high and mighty errand, Your actions speak so loud, I can=92t hear a word you=92re saying." -Greg Graffin (Bad Religion) --000e0cd6e3246da61a048410d45f--