簡體   English   中英

獲取公共靜態無效類以從另一個類讀取數組

[英]Getting a public static void class to read an array from another class

這里是說類

public static void showCodes(){
            System.out.print("  Select from");
            System.out.print("  =>  ");
            for (String show : productDB.inventory)
            { System.out.print(show.toUpperCase() + " ");}
            System.out.print("  <=");
     }

這是另一個類(具有數組)

public class ProductDB
{
public String[] inventory = {"java", "jsps", "mcb2", "txtp", "calc", "topg", "bigl", "sgtp"};
**insert rest of class code**
}

這是我的主要

public static void main(String args[])
{
System.out.println("  Product Selection\n");
showCodes();
  ...

我知道我需要定義我的變量,但我對 Java 還很陌生。 在第 4 行的第一個塊代碼中,出現“找不到符號”錯誤。

將方法showCodes中的for循環更改for

for (String show : ProductDB.inventory)

並將static添加到類中的數組定義中,如下所示:

public static String[] inventory = {"java", "jsps", "mcb2", "txtp", "calc", "topg", "bigl", "sgtp"};

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM