如何打印存储在数组中的字符串值?

时间:2014-10-22 19:40:27

标签: java arrays netbeans

我在NetBeans 8上使用Java。我搜索了谷歌和其他堆栈溢出问题,但我找不到任何与我的问题有关的内容。另外,我已经查阅了oracle文档,他们的示例给了我同样的错误,我遇到了。

我希望下面的数组字符串值使用一行代码打印到输出框,如:

        System.out.println(pbArray[0][0] + pbArray[1][0]);
        ////////////////////////////////////////////////

但无论我在哪里尝试输入该行代码,我都无法使其正常工作。 NetBeans告诉我,预期会有一个标识符。"

package com.mnlottery.console;

public class ComMnlotteryConsole {


public static void main(String[] args) {
     class powerball{

     String[][] pbArray = {
        {"August 16th, 2014","August 13th, 2014","August 9th, 2014"},
        {"07, 08, 17, 48, 69, 09, $50,000,000","08, 37, 39, 40, 52, 24,"
                + " $40,000,000","03, 12, 31, 34, 51, 24, 90,000,000"}};
    }                     
}
}

我觉得这应该很简单。提前致谢。

2 个答案:

答案 0 :(得分:1)

我想你想要(没有内在class

public static void main(String[] args) {
    String[][] pbArray = {
            { "August 16th, 2014", "August 13th, 2014", "August 9th, 2014" },
            { "07, 08, 17, 48, 69, 09, $50,000,000",
                    "08, 37, 39, 40, 52, 24," + " $40,000,000",
                    "03, 12, 31, 34, 51, 24, 90,000,000" } };
    System.out.println(pbArray[0][0] + pbArray[1][0]);
}

但是,您可以通过几种方式打印数组,

  1. Arrays.deepToString(Object[])
  2. 一个循环,Arrays.toString(Object[])
  3. String[][] pbArray = {
            { "August 16th, 2014", "August 13th, 2014", "August 9th, 2014" },
            { "07, 08, 17, 48, 69, 09, $50,000,000",
                    "08, 37, 39, 40, 52, 24," + " $40,000,000",
                    "03, 12, 31, 34, 51, 24, 90,000,000" } };
    // 1.
    System.out.println(Arrays.deepToString(pbArray));
    // or 2.
    for (String [] arr : pbArray) {
        System.out.println(Arrays.toString(arr));
    }
    

答案 1 :(得分:0)

System.out.println(pbarray[0][0] + names[1][0]);更改为

System.out.println(pbArray[0][0] + pbArray[1][0]);

名称和pbarray不存在。 pbArray是正确的标识符。