Saturday, January 21, 2017

Learn Java for Beginner easily ! part 6 : Addition between two input variables

In the article before (Part 5) , I've thaught you guys how to input a data into a variable. In this part of tutorial I'm gonna show you guys how to make a relation between two variables that you will input a data in it. Enough with the chit chat, go type this codes below.


This is an example of a simple program of addition. In this program, you will be asked to input 2 numbers, then this program will show you the addition result of those two numbers you have input before.





Before I explain, I want you guys to run this program just like what I'm about to show you on the pictures below.


After Click 'Run' , this program will ask you to input a number. In this case, I input "5,6". My computer runs in a different basic languange, that's why I'm using comma (,) .
Maybe on your computer you will have to use the full stop (.) sign.

Next, Press 'Enter'.
Then this program will ask you to input the second number just like the picture below.



In this case, I will input "4,4" as the second number.

Next, Press 'Enter'.
Then this program will show you the result of addition of those two number you guys have input just like the pictue below.




This is the explanation of the program  :



Line 1 :

I've explained it on Part 5

Line 3 :

That's where you name your class, I've explained it in Part 2

Line 4 :

I've explained it in Part 2 .

Line 5 :

I've explained it on Part 5 .

Line 6 :


This is where you declared variables where you store the first number (fnum),second number (snum) and the result variable(result).

Line 7  :

This line is to print out a text that tells the user to input a number .

Line 8 :

This code's function is to store the first number that you input into "fnum" variable. The reason why we type "cherry" is because we're gonna use the "cherry" scanner that we have dcclared before and the reason why we type ".nextDouble()" is because I want the input to be in number format.Double is one of data type that I've explained in Part 4 .

Line 9 :

This line is to print out a text that tells the user to input a number .

Line 10 :

The same explanation with Line 8.

Line 12 :

The code above declared that the "result" variable is the addition between the first number and the second number you have input.


Line 14 :
This code is to print out the "result" variable. I typed it without quotation mark is because what we are about to print out is not a String, but a variable which is the result of the addition between two numbers that I have input before.

So, That's it guys !
If you guys have any advice or question. Don'be hesitate to use the comment feature please.

No comments:

Post a Comment