9th Nov 2022, 12:07 PM
Kishan Khanna
Kishan Khanna - avatar
4 Réponses
+ 2
import java.util.Scanner; class arrInput { public static void main(String args[]) { int n; Scanner s = new Scanner(System.in); System.out.println("Enter Length of Array"); n = s.nextInt(); int arr[] = new int[n]; System.out.println("Enter Numbers in Array"); for(int i=0;i<n;i++) { System.out.print("\nEnter " +(i+1)); if(i+1==1){ System.out.print("st ");} else if(i+1==2){ System.out.print("nd ");} else if(i+1==3){ System.out.print("rd ");} else{ System.out.print("th ");} System.out.print("Number:: "); arr[i] = s.nextInt(); System.out.println(arr[i]); } } }
9th Nov 2022, 2:32 PM
Jayakrishna 🇮🇳
+ 2
import java.util.Scanner; class arrInput { public static void main(String args[]) { int n; Scanner s = new Scanner(System.in); System.out.println("Enter Length of Array"); n = s.nextInt(); int arr[] = new int[n]; System.out.println("Enter Numbers in Array"); for(int i=0;i<n;i++) { System.out.print("Enter " +(i+1)); if(i+1==1){System.out.print("st ");} else if(i+1==2){System.out.print("nd ");} else if(i+1==3){System.out.print("rd ");} else{System.out.print("th ");} System.out.println("Number::"); arr[i] = s.nextInt(); } // taken values and passed to method by arrayInput(arr,n); } public static void arrayInput(int arr[],int n){ for( int i=0; i<n; i++) System.out.println(i + " : "+ arr[i]); } }
9th Nov 2022, 2:41 PM
Jayakrishna 🇮🇳
+ 1
You cannot use multiple scanner for input stream in program. You can do with single scanner object. Accept inputs in a single method then pass to other methods if need.
9th Nov 2022, 12:15 PM
Jayakrishna 🇮🇳
0
Can you show me how to?
9th Nov 2022, 1:16 PM
Kishan Khanna
Kishan Khanna - avatar