I can not use for
equal use in Java
.
I'm trying to figure out if a dice is a hook. An honest casino owner has cast%% of them, given the% of results of the rollouts, determine the number of occurrences of each face.
I can not use for
equal use in Java
.
I'm trying to figure out if a dice is a hook. An honest casino owner has cast%% of them, given the% of results of the rollouts, determine the number of occurrences of each face.
The syntax of for
in Ruby is:
for «variável» in «lista»
«comandos»
end
To make a loop from 0 to 9 (10 reps), something equivalent to for(i=0; i<10; i++)
, you will do:
for i in 0..9
puts "o valor de 'i' é #{x}"
end
And if you want, you can use a variable to indicate the boundary of your loop:
n=9
for i in 0..n
puts "o valor de 'i' é #{x}"
end
If you do not want to include the last value, use ...
instead of ..
. In this case you will get the same result using 0..9
or 0...10
.
Rubistas are not fans of explicit ties like for
and while
.
It is very common to see loops using iterators, as does Array#each
:
a = [ "a", "b", "c" ]
a.each {|x| print x, " -- " }
=> a -- b -- c --
Or even using Integer#times
:
5.times do |i|
print i, " "
end
#=> 0 1 2 3 4
Of course, each
or times
does not always solve your problem, but it's rare that I had to use for
or while
in Ruby.