Friday, February 25, 2022

Java a program that the values are type by the user

So now let's do a program that do a usefull thing for the user. This gonna sum 1st the more simple, to an more complex in the future.

In the class that we already choice to be the main, we gonna CODE -->

Inside the main between the brakets of the main{}

We gonna type Scanner, this declaration isn't enought to we use the Scanner, so the IDE gonna show a problem, and gonna ask you to import the class Scanner to your project and you gonna say I do(How those words remit to hapiness, here is yes, we don't compromise more than need in marriage you do your part I do my part no one got in the other life, I do...),

So what make yuo can use the Scanner is the IDE import that for you, so the mere declaration isn't enough to be abitlited the Scanner what make have in your program is the importation, yuo gonna knew when see the Scanner importation in the top of the class out of the class, when I do programs that need Scanner  also in other class, I create a Scanner in every class, so you can't use the object Scanner of the main in other class, in declare Scanner in each class, you gonna named when you created so in different classes no problem if are the same name or different names

See the red-purple arrow I could declare the Scanner-object also there only need that happens before I put the x to wait for the first value.
So the declaration is
Scanner keyb = new Scanner(System.in);
Scanner is the class (class Scanner) keyb is the name of the object that will receive the information typed, so when we put a Scanner we receive power to open fields in the program to receive an entry,
after the = we have new all the objects when created have new, and after the Scanner to afirm that we are creating an Scanner-object, the first Scanner is the way to make an instantiation, so I declare that I am making an instantiation with the first Scanner,
The parentesys have when we are creating objects, is that this class have a parameter that is the entry of info so the object of that class have to have that command between the parenthesys.



So I do a sout to inform to the user that I am waiting an answer, isn't exactly need, but logically how the person would know that I am waiting an action? So I make a sout to ask that action.
After I ask I put just after (the Scanner is the exactly under but I already said that could be before)
the var x to receive the answer.
and the declaration is:
int x = keyb.nextInt();
So the 1st part is the declaration the variable x int the variable by the type, and x is the variable name and the = is atribution, I am making an atribution of the keyb.nextInt,
keyb is the object Scanner that I create to make that interation that I could receive an answer from the user and the .nextInt() this is hard to remember if the IDE don't give the option of auto-complete that piece, is nextInt() because after typed, is what I gonna receive, so the next answer that the user type will be available to me, int of the nextInt is because I am waiting an answer that is an integer, and I gonna storage it in the variable x that is the first part of the declaration,
Have the parenthesys because is a method that is loaded by the keyb, so keyb after the dot the method, of course I have to say what is calling the method is the object keyb, so after the keyb a dot and after the dot the method.
And the code after is just repeat the same steps to the next variable, like is in the picutre above. Rute Bezerra de Menezes Gondim

No comments:

Post a Comment