【问题标题】:Java array what should i do in order to read from the array and display [closed]Java数组我应该怎么做才能从数组中读取并显示[关闭]
【发布时间】:2014-01-14 13:16:47
【问题描述】:

我应该怎么做才能从数组中读取并显示它。 我尝试使用这些,但 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]); 

  }
}

【问题讨论】:

标签: java arrays


【解决方案1】:

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

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

【讨论】:

    猜你喜欢
    • 2022-12-11
    • 1970-01-01
    • 2022-06-13
    • 2021-01-25
    • 1970-01-01
    • 2017-11-22
    • 2010-09-16
    • 2014-11-03
    • 2016-08-28
    相关资源
    最近更新 更多