In: Computer Science
Hi, you cannot use a single method for once to take value and then to print the values. what you can do is you can do both of these work one after the other in a sequence. Like if you call a method it will ask user to input values and after taking all the values it will print all those values right after input from user. If you want to do both of these separately, you have to create two different methods, one for taking input and one for showing the output. Call them whenever you want.
import java.util.Scanner;
public class Test {
void userIO{
int ar[];
Scanner scan = new Scanner(System.in);
for(int i=0; i<5; i++) {
System.out.print("Enter number " + (i+1) + ": ");
ar[i] = scan.nextInt(); // Taking user input
}
for(int i=0; i<5; i++)
System.out.println("Number " + (i+1) + ": " + ar[i]);
}
public static void main(String[] args) {
userIO(); //Calling the method.
}
}
In above example, I have used only one method for both purposes.
Happy Coding.
PLEASE GIVE A THUMBSUP IF IT WAS HELPFULL AND LEAVE A COMMENT FOR ANY QUERY.
THANK YOU..