如何使用java在Array中获取用户输入?

如何使用Java在Array中获取用户输入? 即我们不是在我们的程序中自己初始化它,但用户将给它的价值..请指导!!

这是一个从stdin读取字符串的简单代码,将它们添加到List ,然后使用toArray将其转换为String[] (如果您确实需要使用数组)。

 import java.util.*; public class UserInput { public static void main(String[] args) { List list = new ArrayList(); Scanner stdin = new Scanner(System.in); do { System.out.println("Current list is " + list); System.out.println("Add more? (y/n)"); if (stdin.next().startsWith("y")) { System.out.println("Enter : "); list.add(stdin.next()); } else { break; } } while (true); stdin.close(); System.out.println("List is " + list); String[] arr = list.toArray(new String[0]); System.out.println("Array is " + Arrays.toString(arr)); } } 

也可以看看:

  • 为什么在Java中使用Lists而不是Arrays?
  • 使用List数据填充数组
 package userinput; import java.util.Scanner; public class USERINPUT { public static void main(String[] args) { Scanner input = new Scanner(System.in); //allow user input; System.out.println("How many numbers do you want to enter?"); int num = input.nextInt(); int array[] = new int[num]; System.out.println("Enter the " + num + " numbers now."); for (int i = 0 ; i < array.length; i++ ) { array[i] = input.nextInt(); } //you notice that now the elements have been stored in the array .. array[] System.out.println("These are the numbers you have entered."); printArray(array); input.close(); } //this method prints the elements in an array...... //if this case is true, then that's enough to prove to you that the user input has //been stored in an array!!!!!!! public static void printArray(int arr[]){ int n = arr.length; for (int i = 0; i < n; i++) { System.out.print(arr[i] + " "); } } } 
 import java.util.Scanner; class bigest { public static void main (String[] args) { Scanner input = new Scanner(System.in); System.out.println ("how many number you want to put in the pot?"); int num = input.nextInt(); int numbers[] = new int[num]; for (int i = 0; i < num; i++) { System.out.println ("number" + i + ":"); numbers[i] = input.nextInt(); } for (int temp : numbers){ System.out.print (temp + "\t"); } input.close(); } } 

它在很大程度上取决于您打算如何获取此输入,即您的程序打算如何与用户交互。

最简单的示例是,如果要捆绑可执行文件 – 在这种情况下,用户只需在命令行上提供数组元素,就可以从应用程序的main方法访问相应的数组。

或者,如果您正在编写某种Web应用程序,则需要接受应用程序的doGet / doPost方法中的值,方法是手动解析查询参数,或者通过为提交到解析的HTML表单提供服务的用户页。

如果是Swing应用程序,您可能需要弹出一个文本框供用户输入输入。 在其他情况下,您可以从数据库/文件中读取值,这些值先前已由用户存放。

基本上, 一旦你找到了获取输入的方法, 输入作为数组读取就很容易 。 您需要考虑应用程序运行的上下文,以及用户可能希望如何与此类应用程序进行交互,然后决定有意义的I / O体系结构。

**如何通过用户输入接受数组

回答:-

 import java.io.*; import java.lang.*; class Reverse1 { public static void main(String args[]) throws IOException { int a[]=new int[25]; int num=0,i=0; BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); System.out.println("Enter the Number of element"); num=Integer.parseInt(br.readLine()); System.out.println("Enter the array"); for(i=1;i<=num;i++) { a[i]=Integer.parseInt(br.readLine()); } for(i=num;i>=1;i--) { System.out.println(a[i]); } } } 

您可以执行以下操作:

 import java.util.Scanner; public class Test { public static void main(String[] args) { int arr[]; Scanner scan = new Scanner(System.in); // If you want to take 5 numbers for user and store it in an int array for(int i=0; i<5; i++) { System.out.print("Enter number " + (i+1) + ": "); arr[i] = scan.nextInt(); // Taking user input } // For printing those numbers for(int i=0; i<5; i++) System.out.println("Number " + (i+1) + ": " + arr[i]); } } 

import java.util.Scanner;

class Example {

//检查字符串是否被视为整数。

 public static boolean isInteger(String s){ if(s.isEmpty())return false; for (int i = 0; i  

}