Java数组我应该怎么做才能从数组和显示中读取

时间:2014-01-14 13:16:47

标签: java arrays

我该怎么做才能从数组中读取并显示它。 我尝试使用这些,但input.next();

有一个错误
import java.util.*;

public class StringeEX
{
  static Scanner input = new Scanner(System.in).useDelimiter("\r\n");


  public static void main(String[] args)
  {     
    String ColourOne[] ={"Black","Brown","Red","Orange","Yellow","Green","Blue","Violet","Grey","White"};

    System.out.println("==========================================================");
    System.out.println("Black  = 0," + "\t" + "Brown = 1," + "\t" + "Red   = 2,"+ "\t" +"Orange = 3");
    System.out.println("Yellow = 4," + "\t" + "Green = 5," + "\t" + "Blue  = 6," + "\t" +"Violet = 7");
    System.out.println("*** NOTE: ***"+"\t"+"Grey  = 8,"+ "\t"+"white = 9");
    System.out.println("==========================================================");
    System.out.print("Please enter the number for the colour :");
    ColourOne = input.next();
 for(int i = 0; i < ColourOne.length; i++) 
     System.out.println(ColourOne[i]); 

  }
}

1 个答案:

答案 0 :(得分:0)

您正尝试将扫描仪输入存储到阵列中。你应该指定一个单独的变量。数组是零索引的,所以当从数组中获取值时,使用ArrayName[someNumber],其中someNumber引用数组中的元素。第一个元素从0开始。

int i = input.nextInt();
System.out.println(ColourOne[i]);