Java使用scanner访问数组元素

Java使用scanner访问数组元素,java,arrays,Java,Arrays,我使用scanner类创建了一个8个元素长的数组。我试图通过扫描仪接受用户输入,并打印出索引位置的值。例如,如果用户输入“2”,它将在第二个元素处打印出值。我在google上搜索过,但所有的资源都是如何使用扫描仪将数据输入数组,而不是使用扫描仪检索数据。我想到的唯一方法是使用大量荒谬的if-else语句,但必须有一种更干净的方法在循环中实现它 这是我的阵列,我用扫描仪填充了阵列。现在,在提示下,用户必须输入1到8之间的数字。如果输入1,则打印出解决方案[0]。输入8,打印解决方案[7]等。希望现

我使用scanner类创建了一个8个元素长的数组。我试图通过扫描仪接受用户输入,并打印出索引位置的值。例如,如果用户输入“2”,它将在第二个元素处打印出值。我在google上搜索过,但所有的资源都是如何使用扫描仪将数据输入数组,而不是使用扫描仪检索数据。我想到的唯一方法是使用大量荒谬的if-else语句,但必须有一种更干净的方法在循环中实现它

这是我的阵列,我用扫描仪填充了阵列。现在,在提示下,用户必须输入1到8之间的数字。如果输入1,则打印出解决方案[0]。输入8,打印解决方案[7]等。希望现在更容易理解

    String[] solution = new String[8];
    Scanner scan = new Scanner( System.in );
    for( int i = 0; i < solution.length; i++ ){
        System.out.println( "Enter solution:" );
        solution[ i ] = scan.next();
    }
    scan.close();
    Scanner scan1 = new Scanner( System.in );
    String selection;

    System.out.println("Enter an int between 0 and 7 to retrieve the Selection: ");
    selection = scan1.next();
    int i = Integer.parseInt(selection);

    System.out.println( "The Selection is: " + solution[i] );
String[]解决方案=新字符串[8];
扫描仪扫描=新扫描仪(System.in);
for(int i=0;i
您必须读取一个int,并在获取存储在指定索引中的值时使用它

实现这一点的方法如下:

yourArray[scanner.nextInt()];
其中
scanner
scanner
对象

要了解在阅读你认为是数字的东西时可能收到的例外情况,你可以这样做:

Scanner scanner = new Scanner(System.in);
try {
    yourArray[scanner.nextInt()];

} catch(IllegalStateException | NoSuchElementException e) { // <--- Java 1.7 syntax, in case you wonder
    e.printStackTrace();
}
Scanner Scanner=新的扫描仪(System.in);
试一试{
数组[scanner.nextInt()];
}catch(非法状态异常| NoSuchElementException e){//
然后输出:

urArray[input.nextInt()];

如果没有任何代码,这是很困难的,但是基本上,使用扫描器将输入获取到字符串
selection
,使用
int i=integer.parseInt(selection);
,将整数值获取到
int i
,然后
myArray[i]
import java.util.scanner;
 import java.util.Scanner;
 public class array1
 {
 public static void main(String args[])
 {
 int arr[]=new arr[100];
 int i;
 Scanner sc=new Scanner(System.in);
 System.out.pritln("Enter the Number of Element less than 100");
 int a=sc.nextInt();
 System.out.println("Enter the Number");
 for(i=0;i<a;i++)
 {
 arr[i]=sc.nextInt();
 }
 System.out.println("List of Elements in array");
for(i=0;i<a;i++)
{
System.out.println(arr[i]);
}
}
}
公共类数组1 { 公共静态void main(字符串参数[]) { int arr[]=新arr[100]; int i; 扫描仪sc=新的扫描仪(System.in); System.out.pritln(“输入小于100的元素数”); int a=sc.nextInt(); System.out.println(“输入编号”);
对于(i=0;i现在我必须将值存储为字符串。数组的索引号是否未归类为int?我同时考虑了两种不同的解决方案。我更新了答案以更好地解释应该执行的操作。我收到了yourarray[scanner.nextInt()]的错误;上面发布的更新代码添加了一些代码,尽管我怀疑它是否有用。为什么我必须存储为字符串并将其解析为int,而不是首先将其作为int?因为我忘记了
Scanner.nextInt()
:)
Integer.parseInt()
是我通常从命令行参数中获取
int
值的方法。请解释您的答案。SO的存在不仅是为了回答问题,也是为了教人们如何和为什么编码。
 import java.util.Scanner;
 public class array1
 {
 public static void main(String args[])
 {
 int arr[]=new arr[100];
 int i;
 Scanner sc=new Scanner(System.in);
 System.out.pritln("Enter the Number of Element less than 100");
 int a=sc.nextInt();
 System.out.println("Enter the Number");
 for(i=0;i<a;i++)
 {
 arr[i]=sc.nextInt();
 }
 System.out.println("List of Elements in array");
for(i=0;i<a;i++)
{
System.out.println(arr[i]);
}
}
}