0
What's wrong here
4 Respostas
+ 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]);
}
}
}
+ 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]);
}
}
+ 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.
0
Can you show me how to?