method that stores inputs into an array [closed]
I am trying to write a method that prompts the user to input 20 numbers开发者_开发百科 and stores the numbers in an alpha array. For some reason it is not working. Below is a sample of my method:
public void inputArray(int a,int[]b){
b=new int[20];
int n=0;//int a;
while(n<5){
for(int i=0;i<alpha.length;i++){
//a=window.readInt("please enter no :");
alpha[n]=a;
n++;
}
}
}
I am writing a sample java code
import java.util.*;
void input(int []a)
{
Scanner sc =new Scanner(System.in);
for(int i=0;i<20;i++)?
{
System.out.println("Enter a number ="):
a[i]= sc.nextInt();
}
}
This java code is not complete .You can Google for How To input data from User for your specific programming language.
You must use Scanner or BufferReader as you wish, I thinks thats the simple way of doing I hope I can help
import java.util.Scanner;
public class Prueba {
static int alpha[] = new int[20];
static Scanner scan = new Scanner(System.in);
public static void main(String args[]) {
int array[] = new int[20];
array = inputArray(alpha);
}
public static int[] inputArray(int alpha[]) {
for (int i = 0; i < alpha.length; i++) {
alpha[i] = scan.nextInt();
System.out.println(alpha[i]);
}
return alpha;
}
}
精彩评论