[ruby] drill output

Contents

image.png

Implementation

image.png

Commentary

This time, I created a program that adds numbers from 1 to 10 in order. Below is the code I first tried. image.png The output is below. image.png This program fails because it is 1 + 2 + 3 + 4 + 5 ...

Then what should I do (-_-;) Let's preach one by one

① Prepare a variable sum to save the total value (2) Add numerical values from 1 to 10 to the variable sum in order. (3) Replace the process of adding in order by repeating the times statement. ④ Output the value of the variable sum to the terminal

First is ①. Add 2 to 1 Add 3 to the result Add 4 to the result Add 5 to the result, and repeat until 10.

Therefore, we need a variable to always save the result. Define this as the variable name sum. Since nothing has been added yet, 0 is assigned to sum.

(2) Add numerical values from 1 to 10 to the variable sum in order. Let's write a program that adds numerical values from 1 to 10 to the variable sum in order. image.png This code is very long and the same process continues, so use the times statement to rewrite it into efficient code.

(3) Replace the process of adding in order by repeating the times statement. You can use the times statement to put together the same process that is repeated many times. This time, the sum + = numerical part is summarized.

In the times statement, write the number of times you want to repeat .times to determine the number of repetitions. This time, 1 to 10 are added in order, so the same process is repeated 10 times. Therefore, I want to repeat it 10 times. image.png What should I do with the numerical part? This number changes as the number of repetitions increases. The first time is 1, the second time is 2, and the third time is 3. In other words, it is the same as the number of repetitions. In the times statement, the number of repetitions is automatically assigned as a numerical value in the variable i. Therefore, the variable i can be used to add the number of repetitions to the variable sum. However, since it is a program, the value of i for the first time will be 0. Therefore, add 1 to i + 1. image.png ④ Output the value of the variable sum to the terminal Finally, output sum to the terminal with the puts method and you're done.

Recommended Posts

[ruby] drill output
[ruby] drill output
[ruby] drill output
[ruby] drill output
[ruby] drill output
Ruby input / output
Ruby standard output
Ruby logical operator drill
Ruby calorie calculation output
Output triangle in Ruby
[Ruby on Rails] CSV output function
Ruby learning 4
[Ruby] Array
Ruby learning 5
Ruby basics
Ruby Review 2
Ruby addition
Refactoring Ruby
Ruby learning 3
2020/10/4 Morning output
Ruby print puts p printf output method
2020/10/4 noon output
Learning output ~ 11/3 ~
Ruby setting 2
Ruby problem ⑦
2020/10/5 Morning output
Ruby learning 2
[Ruby] Block
Refactoring Ruby
ruby calculator
Ruby learning 6
Ruby settings 1
Refactoring Ruby
Learning output
2020/10/3 Noon output
Ruby basics
Ruby memo
Ruby learning 1
Ruby Review 1
From terminal to Ruby (standard input / output)
2020/10/3 Morning output
[Ruby] Module
Drill (self-made)
[Ruby] Difference between puts and return, output and return value