Printing Table in JRuby
In the previous sections of JRuby tutorials you have studied how to run program in JRuby and to calculate factorial in JRuby. Now in this part of JRuby tutorial we will tell you how to take input in JRuby from the console and print output table on the console output.
This example to print table will take input from the
console and this is being done with the "gets" method. To print
table we have used loop control
"upto...do...end". Here is the example code for generating
table in JRuby as follows:
JRubyTable.java
# Printing Table in JRuby print " Enter number to print table := " n= gets 1.upto(10) do |i| puts "#{n} * #{i} = #{n.to_i * i.to_i}" end |
To execute this program execute this JRubyTable.rb with jruby command. This will ask for a value to calculate factorial and generate result according to given value.
Output:
C:\JRuby>jruby JRubyTable.rb Enter number to print table := 6 6 * 1 = 6 6 * 2 = 12 6 * 3 = 18 6 * 4 = 24 6 * 5 = 30 6 * 6 = 36 6 * 7 = 42 6 * 8 = 48 6 * 9 = 54 6 * 10 = 60 |