ruby打印_Ruby程序打印一个数字的乘法表

ruby打印

打印乘法表 (Printing multiplication table)

This requires a very simple logic where we only have to multiply the number with digits from 1 to 10. This can be implemented by putting the multiplication statement inside a loop. We have mentioned two ways: one is by using while loop and the second one is by making use of for loop. When you are using while loop, first you will have to initialize i with 1 and increment it by 1 inside the loop. for loop, the method is simpler as it only requires the specification of for keyword along with the range on which the loop is going to work.

这需要非常简单的逻辑,其中我们只需要将数字与1到10的数字相乘即可。这可以通过将乘法语句放入循环中来实现。 我们提到了两种方法:一种是使用while循环,第二种是使用for循环 。 使用while循环时,首先必须在循环内将i初始化为1并将其递增1。 for循环 ,该方法更简单,因为它只需要指定for关键字以及循环将要作用的范围。

Methods used:

使用的方法:

  • puts: This is a predefined method which is used to put the string on the console.

    puts :这是一种预定义的方法,用于将字符串放置在控制台上。

  • gets: This is also a predefined method in Ruby library which is used to take input from the user through the console in the form of string.

    gets :这也是Ruby库中的预定义方法,用于通过控制台以字符串形式从用户获取输入。

  • *: This is an arithmetic operator commonly known as multiplication operator which takes two arguments and process them by giving out their product as a result.

    * :这是一种算术运算符,通常称为乘法运算符,它接受两个参数,并通过将其结果作为乘积进行处理。

Variables used:

使用的变量:

  • num: This variable is used to store the integer provided by the user.

    num :此变量用于存储用户提供的整数。

  • mult: This is storing the result for i*num.

    mult :这将存储i * num的结果。

  • i: This is a loop variable which is initialized by 1.

    i :这是一个由1初始化的循环变量。

Ruby代码打印数字的乘法表 (Ruby code to print multiplication table of a number)

=begin
Ruby program to print multiplication table of 
a number(by using for loop)
=end
puts "Enter the number:"
num=gets.chomp.to_i
for i in 1..10
mult=num*i
puts "#{num} * #{i} = #{mult}"
end

Output

输出量

Enter the number:
13
13 * 1 = 13
13 * 2 = 26
13 * 3 = 39
13 * 4 = 52
13 * 5 = 65
13 * 6 = 78
13 * 7 = 91
13 * 8 = 104
13 * 9 = 117
13 * 10 = 130

Method 2:

方法2:

=begin
Ruby program to print multiplication table of a 
number(by using while loop)
=end
puts "Enter the number:"
num=gets.chomp.to_i
i=1
while (i<=10)
mult=num*i
puts "#{num} * #{i} = #{mult}"
i+=1
end

Output

输出量

Enter the number:
16
16 * 1 = 16
16 * 2 = 32
16 * 3 = 48
16 * 4 = 64
16 * 5 = 80
16 * 6 = 96
16 * 7 = 112
16 * 8 = 128
16 * 9 = 144
16 * 10 = 160

Code explanation:

代码说明:

The logic of code is pretty simple. In the first method, we are using while loop for the process and in the second one, we are using for loop. We have a variable mult in which we are multiplying the number with the i. The loop will terminate when i becomes equal to 10.

代码的逻辑非常简单。 在第一种方法中,我们使用while循环进行处理,在第二种方法中,我们使用for循环 。 我们有一个变量mult ,其中我们将数字乘以i 。 当我等于10时,循环将终止。

翻译自: https://www.includehelp.com/ruby/print-multiplication-table-of-a-number.aspx

ruby打印

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/541761.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

步骤1:JMeter 录制脚本接口测试

JMeter 常用测试方法简介 1.下载安装 http://jmeter.apache.org/download_jmeter.cgi 安装JDK&#xff0c;配置环境变量JAVA_HOME. 系统要求&#xff1a;JMeter2.11 需要JDK1.6以上的版本支持运行 2.学习Jmeter元件 http://jmeter.apache.org/usermanual/component_reference.h…

模拟断电oracle数据不一致,Oracle数据库案例整理-Oracle系统运行时故障-断电导致数据文件状态变为RECOVER...

1.1 现象描述异常断电&#xff0c;数据库数据文件的状态由ONLINE变为RECOVER。系统显示如下信息&#xff1a;SQL> select file_name ,tablespace_name ,online_status from dba_data_files;FILE_NAME---------------------------------------------------------------…

python日历模块_Python日历模块| prmonth()方法与示例

python日历模块Python calendar.prmonth()方法 (Python calendar.prmonth() Method) prmonth() method is an inbuilt method of the calendar module in Python. It works on simple text calendars and prints the calendar of the given month of the given year. Also, the…

多例模式

多例&#xff1a;只是单例的一种延伸 不必过于在意各种模式的名字&#xff0c;重要的是学会融会贯通&#xff0c;把生产的car放到集合中 类似JDBC 的连接池 把连接对象放到池中 多例模式特点&#xff1a; 1. 多例类可以有多个实例 2. 多例类必须自己创建自己的实例&a…

Oracle public view,【易错概念】以太坊Solidity函数的external/internal,public/private,view/pure/payable区别...

1. 函数类型&#xff1a;内部(internal)函数和外部(external)函数函数类型是一种表示函数的类型。可以将一个函数赋值给另一个函数类型的变量&#xff0c;也可以将一个函数作为参数进行传递&#xff0c;还能在函数调用中返回函数类型变量。 函数类型有两类&#xff1a;- 内部(i…

c-style字符字符串_C字符串-能力问题与解答

c-style字符字符串C programming String Aptitude Questions and Answers: In this section you will find C Aptitude Questions and Answers on Strings, String is the set of characters and String related Aptitude Questions and Answers you will find here. C编程Stri…

PHP Smarty template for website

/******************************************************************************* PHP Smarty template for website* 说明&#xff1a;* 之前一直在想将MVC的方式加在PHP做的网站上&#xff0c;这样比较好处理&#xff0c;相对来说比较好* 处理…

ftp连接oracle服务器,使用SSL加密连接FTP - 架建SSL安全加密的FTP服务器(图)_服务器应用_Linux公社-Linux系统门户网站...

四、使用SSL加密连接FTP启用Serv-U服务器的SSL功能后&#xff0c;就可以利用此功能安全传输数据了&#xff0c;但FTP客户端程序必须支持SSL功能才行。 如果我们直接使用IE浏览器进行登录则会出现图4显示的错误信息&#xff0c;一方面是以为没有修改默认的端口21为990&#xff0…

c# 情感倾向_C否则-能力倾向问题与解答

c# 情感倾向C programming if else Aptitude Questions and Answers: In this section you will find C Aptitude Questions and Answers on condition statements – if else, nested if else, ladder if else, conditional operators etc. C语言编程如果有问题&#xff0c;请…

springboot中使用缓存shiro-ehcache

在pom.xml中注入缓存依赖&#xff0c;版本(Sep 09, 2016)spring-context-support 包含支持UI模版&#xff08;Velocity&#xff0c;FreeMarker&#xff0c;JasperReports&#xff09;&#xff0c; 邮件服务&#xff0c; 脚本服务(JRuby)&#xff0c; 缓存Cache&#xff08;EHCa…

oracle 微信公众号,关于微信公众号贴代码的方法

微信公众号码上贴代码一直一来都是个头疼的问题。吐槽一句&#xff1a;要是后台编辑器支持markdown就好了。今天教大家用在线markdown排版工具&#xff0c;把代码完美贴到微信公众号上。长话短说&#xff0c;今天用到的两个工具&#xff1a;首先&#xff0c;以一段代码为例。假…

计算理论 形式语言与自动机_下推式自动机(PDA)| 计算理论

计算理论 形式语言与自动机Pushdown Automaton (PDA) is a kind of Automaton which comes under the theory of Computation that appoints stack. The word Pushdown stands due to the fact that the stack can be pushed down as operations can only work on the elements…

运维人员究竟如何提升价值,持续获得高薪?

作者简介&#xff1a;老男孩&#xff0c;北京老男孩IT教育创始人&#xff0c;17年IT经验&#xff0c;资深Linux实战专家&#xff0c;IT培训界实战派顶尖大师&#xff0c;国内将实战心理学体系大量注入IT运维培训领域的第一人&#xff0c;多本IT畅销图书作者&#xff0c;51CTO金…

Webservice soap wsdl区别之个人见解

Web Service实现业务诉求&#xff1a;Web Service是真正“办事”的那个&#xff0c;提供一种办事接口的统称。WSDL提供“能办的事的文档说明”&#xff1a;对要提供的服务的一种描述格式。我想帮你的忙&#xff0c;但是我要告诉你我都能干什么&#xff0c;以及干这些事情需要的…

java uuid静态方法_Java UUID nameUUIDFromBytes()方法及示例

java uuid静态方法UUID类名UUIDFromBytes()方法 (UUID Class nameUUIDFromBytes() method) nameUUIDFromBytes() method is available in java.util package. java.util包中提供了nameUUIDFromBytes()方法 。 nameUUIDFromBytes() method is used to get a UUID constructed fr…

清空 linux 服务器,Linux服务器清理

Why?废话不多说直接来图&#xff0c;可以看出磁盘已经快要满了未清之前What?可以看出mnt文件夹占用的最大&#xff0c;然后进入mnt目录里通过命令,根据文件大小对该路径下文件排序du -h --max-depth1我们服务器出现磁盘快满了的原因是因为&#xff0c;服务器部署了多个tomcat…

Git中的AutoCRLF与SafeCRLF换行符问题

2019独角兽企业重金招聘Python工程师标准>>> 原文&#xff1a;http://www.cnblogs.com/flying_bat/archive/2013/09/16/3324769.html 最近在使用GitHub&#xff0c;发现不时没有修改过的文件要提交&#xff0c;对比发现文件全部修改&#xff0c;但找不到不一样的地方…

stringwriter_Java StringWriter getBuffer()方法与示例

stringwriterStringWriter类的getBuffer()方法 (StringWriter Class getBuffer() method) getBuffer() method is available in java.io package. getBuffer()方法在java.io包中可用。 getBuffer() method is used to get the StringBuffer that holds the present buffer valu…

linux 下邮件服务器,Linux 下搭建Postfix邮件服务器

Linux 下搭建Postfix邮件服务器详解&#xff1a;1、首先关闭sendmail服务service sendmail stop2、chkconfig sendmail off(关闭开机自启动)3、修改DNS正解文件&#xff0c;使DNS能够解析邮箱服务添加下面两行mail.zhubf.com. IN A 172.17.17.2zhubf.com. IN M…

Java PipedInputStream close()方法与示例

PipedInputStream类close()方法 (PipedInputStream Class close() method) close() method is available in java.io package. close()方法在java.io包中可用。 close() method is used to close this PipedInputStream and free all system resources linked with this stream…