+ 1
Can we ask a question to the user and take input from the answer we got in JAVA? If possible, how?
2 Réponses
+ 2
use a scanner.
-----------------
System.out.println("What is your name?");
Scanner input = new Scanner(System.in);
String name = input.nextLine();
System.out.println("Your name is " + name);
+ 1
If you intend to have alot of input, you can simplify it. make it a method like this and build your question right into it...
FOR STRING INPUT -
public void String inputString(String A){
System.out.println(A);
Scanner input = new Scanner(System.in);
String in = input.nextLine();
return in;
}
FOR INTEGER INPUT -
public void int inputInt(String A){
System.out.println(A);
Scanner input = new Scanner(System.in);
int in = input.nextInt();
return in;
}
Now use the methods with built in questions -
String job = inputString("What do you do?");
String name = inputString("What is your name?");
int age = inputInt("How old are you?");
int weight = inputInt("How Much do you weigh?");