如何解决javaoop中找不到或无法加载主类的问题?

vc9ivgsu  于 2021-07-06  发布在  Java
关注(0)|答案(2)|浏览(433)

我的程序要写java实现类,我写了4个文件,
mycompanymain->这将显示其他文件的输出。
company->这将是company.java,其中包含公司的名称。
地址->这将包含为公司工作的人的地址。
供应商->这将链接两个类[公司,地址]。
我有这些错误:

MyCompanyMain.java:29: error: bad initializer for for-loop
        for(Suppliers : company.getSuppliers())
            ^
1 error
Error: Could not find or load main class MyCompanyMain
Caused by: java.lang.ClassNotFoundException: MyCompanyMain

这是mycompanymain.java:

import java.util.ArrayList;
import static java.lang.System.out;
public class MyCompanyMain 
{
    public static void main(String[] args)
    {
        Company company = new Company();
        company.setName("ABC sdn bhd");
        Address address = new Address(123, "Jalan UTeM", "Durian Tunggal",76100, "Melaka", 06123456);
        company.setAddress(address);
        ArrayList<Supplier> suppliers = new ArrayList<Supplier>();
        Supplier supplierBuku = new Supplier();
        supplierBuku.setSupplierName("Syarikat Buku Sdn Bhd");
        Address supplierBukuAddress = new Address(3, "Jalan Munsyi", "Ayer Keroh", 75400, "Melaka", 06123123);
        supplierBuku.setAddress(supplierBukuAddress);
        Supplier supplierComputer = new Supplier();
        supplierComputer.setSupplierName("Syarikat Computer Sdn Bhd");
        Address supplierComputerAddress = new Address(3, "Jalan Bukit Beruang","Ayer Keroh", 75400, "Melaka", 06111223);
        supplierComputer.setAddress(supplierComputerAddress);

        suppliers.add(supplierBuku);
        suppliers.add(supplierComputer);

        Company.setSuppliers(suppliers);

        Address comp_Address = company.getAddress();
        out.println(comp_Address.getNoShop()+" :"+comp_Address.getRoad()+" : "+comp_Address.getDistrict()+" :"+comp_Address.getPhone());

        for(Suppliers : company.getSuppliers())
        {
            out.print(s.getSupplierName()+" \t: ");
            Address supp_Address = s.getAddress();
            out.println(supp_Address.getNoShop()+" :"+supp_Address.getRoad()+" : "+supp_Address.getDistrict()+" :"+supp_Address.getPhone());

        }
    }
}

这是company.java:

import java.util.* ;
public class Company {
   private String name ;
   private Address address ;
   private ArrayList<Supplier> suppliers ;
   public Company() {
       suppliers = new ArrayList<Supplier>();
   }
   public String getName() {
       return name;
   }
   public void setName(String name) {
       this.name = name;
   }
   public Address getAddress() {
       return address;
   }
   public void setAddress(Address address) {
       this.address = address;
   }
   public ArrayList<Supplier> getSuppliers() {
       return suppliers;
   }
   public void setSuppliers(ArrayList<Supplier> suppliers) {
       this.suppliers = suppliers;
   }

}

这是address.java:

public class Address {
   private int noShop ;
   private String road ;
   private String district ;
   private int posCode ;
   private String state ;
   private int phone ;
   public Address(int noShop, String road, String district, int posCode, String state, int phone) {

       this.noShop = noShop;
       this.road = road;
       this.district = district;
       this.posCode = posCode;
       this.state = state;
       this.phone = phone;
   }
   public int getNoShop() {
       return noShop;
   }
   public String getRoad() {
       return road;
   }
   public String getDistrict() {
       return district;
   }
   public int getPosCode() {
       return posCode;
   }
   public String getState() {
       return state;
   }
   public int getPhone() {
       return phone;
   }

}

这是supplier.java:

public class Supplier {
   private String supplierName ;
   private Address address ;
   public Supplier() {

   }
   public String getSupplierName() {
       return supplierName;
   }
   public void setSupplierName(String supplierName) {
       this.supplierName = supplierName;
   }
   public Address getAddress() {
       return address;
   }
   public void setAddress(Address address) {
       this.address = address;
   }

}
zvms9eto

zvms9eto1#

这里的问题不是 main 方法,这是因为您的代码无法编译,因为您缺少增强for循环的类型声明。你好像少了一个间隔 Supplier (类型)和 s (声明的变量):

for(Supplier s : company.getSuppliers())
// Here ----^
{
    out.print(s.getSupplierName()+" \t: ");
    Address supp_Address = s.getAddress();
    out.println(supp_Address.getNoShop()+" :"+supp_Address.getRoad()+" : "+supp_Address.getDistrict()+" :"+supp_Address.getPhone());        
}
au9on6nz

au9on6nz2#

for循环中缺少一个变量,请使用

for(Suppliers supplier : company.getSuppliers()) {
    // rest of code here
}

相关问题