Java, Is is possible to make the User Input affect an int variable?
I will be trying to produce a program that requires you your real age.And i would like to an individual switch varibles or even cases EX:
int time;
time = 3 OR MORE;
move (age)
situation 1:
Process.out.println(“You might crawl”);
separate;
situation 2:
Process.out.println(“You might talk”);
separate;
But this question requires “What is the best age” is it possible to make precisely what you individual inputs transform the int (age)
Of course.A course wouldn’t be very beneficial if them couldn’t find user insight!
I recommend using this Scanner category.Import this, define the particular object (keyboard), prompt and get user input’
significance java.util.Shield;
Shield kb = brand-new Scanner(System.in); // this kind of says find input via keyboard
Process.out.println(“Enter time:”);
int time = kb.nextInt();
I’m surprised that you simply would perhaps ask a real question.Quit sending him text messages and be aware in category.
Yes, you have available the Scanning device class in order to read a great int coming from standard in (which, at the moment, you can visualize as your keyboard).
Basically it’s something such as this:
Shield scanner = innovative Scanner(System.in);
int time = reader.nextInt();
(obviously you’d prompt the user before that).
Most likely don’t care a lot of, but in making your program robust make sure you catch the case that a computer owner doesn’t put in place an integer…but Now i am not commencing that, essentially just do a attempt catch affirmation, if you will have never heard of that, then avoid using it, just assume the consumer will be “well-behaved”.
There will be two common methods of do them.Remove “int age; age = 3 OR MORE; ” and also use any with the following signal.
1).
BufferedReader bufferedreader = brand new BufferedReader(new InputStreamReader(System.in));
String number = bufferedreader.readLine();
int cost = Integer.parseInt(number);
2).
significance java.util.Shield; //Put this along at the top associated with source record.
Shield scan = innovative Scanner(System.in);
int my partner and i = check.nextInt();
Desire this assistance!
try this
significance java.util.*;
training Example
Shield scan = innovative Scanner(System.in);
emptiness fn()
Process.out.println(“What ‘s your age”);
int time = check.nextInt();
move (age)
situation 1:
Process.out.println(“You might crawl”);
separate;
situation 2:
Process.out.println(“You might talk”);
separate;
Desire this assistance!
Absolutely.You’d input into the age changing.(Sorry I can’t provide you with the correct code, but I’m not only a Java programmer.C++ as a substitute.)
Desire that facilitates.
Leave a Reply
You must be logged in to post a comment.