日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

ruby打印_Ruby程序打印数字的力量

發布時間:2023/12/1 编程问答 43 豆豆
生活随笔 收集整理的這篇文章主要介紹了 ruby打印_Ruby程序打印数字的力量 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

ruby打印

Ruby中數字的冪 (Power of a number in Ruby)

The task to develop a program that prints power of a number in Ruby programming language.

開發可以用Ruby編程語言打印數字冪的程序的任務。

If we want to calculate the power of a number manually then we have to multiply the base to itself by exponent times which means that if the base is 3 and the exponent is 4, then power will be calculated as

如果要手動計算數字的冪,則必須將底數乘以自身乘以指數時間,這意味著如果底數為3且指數為4 ,則冪將計算為

power = 3*3*3*3, which will result in 81.

power = 3 * 3 * 3 * 3 ,結果為81。

Let us put the above logic into codes. We have used two methods to calculate power, one is by using user-defined function “pow” and one is with the help of ** operator. If you want to write code from scratch then the first method is appropriate for you.

讓我們將以上邏輯放入代碼中。 我們使用了兩種方法來計算功率,一種方法是使用用戶定義的函數“ pow”,另一種方法是借助**運算符。 如果您想從頭開始編寫代碼,則第一種方法適合您。

Methods used:

使用的方法:

  • puts: This is used to create an interaction with the user by putting some message on the console.

    puts :用于通過在控制臺上放置一些消息來與用戶進行交互。

  • gets: This is used to take input from the user in the form of string.

    gets :用于接收用戶以字符串形式的輸入。

  • to_i: This method is used to convert any type into an integer type.

    to_i :此方法用于將任何類型轉換為整數類型。

  • pow: This is a user-defined function which takes two arguments and returns an integer value. It is defined purposefully for calculating power by taking base and exponent as an argument.

    pow :這是一個用戶定義的函數,它帶有兩個參數并返回一個整數值。 它的定義是有目的的,以底數和指數為參數來計算功效。

Ruby代碼來計算數字的冪 (Ruby code to calculate power of a number)

=begin Ruby program to calculate power of a number. =enddef pow(a,b)power=1for i in 1..bpower=power*aendreturn power endputs "Enter Base:-" base=gets.chomp.to_iputs "Enter exponent:-" expo=gets.chomp.to_iputs "The power is #{pow(base,expo)}"

Output

輸出量

RUN 1: Enter Base:- 3 Enter exponent:- 5 The power is 243RUN 2: Enter Base:- 2 Enter exponent:- 3 The power is 8

Method 2:

方法2:

=begin Ruby program to calculate power of a number using ** operator. =endputs "Enter Base:-" base=gets.chomp.to_iputs "Enter exponent:-" expo=gets.chomp.to_ipower=base**expo puts "The power is #{power}"

Output

輸出量

RUN 1: Enter Base:- 5 Enter exponent:- 5 The power is 3125RUN 2: Enter Base:- 9 Enter exponent:- 2 The power is 81

翻譯自: https://www.includehelp.com/ruby/print-power-of-a-number.aspx

ruby打印

總結

以上是生活随笔為你收集整理的ruby打印_Ruby程序打印数字的力量的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。