Java 要查找输入的项目数?

Java 要查找输入的项目数?,java,Java,我是一名java初学者,在下面的程序中,我为一家cd商店编写了一个程序,对商店中可用的cd进行重新排序,我想知道输入的cd详细信息的总数如何查找 public class Cdshop { int cid; String title; int price; String type; Cdshop() { cid = 100; title = "Avengers"; price = 100;

我是一名java初学者,在下面的程序中,我为一家cd商店编写了一个程序,对商店中可用的cd进行重新排序,我想知道输入的cd详细信息的总数如何查找

public class Cdshop {
    int cid;
    String title;
    int price;
    String type;

    Cdshop() {
        cid = 100;
        title = "Avengers";
        price = 100;
        type = "Action";
    }

    Cdshop(int num1, String str, int num2, String str1) {
        cid = num1;
        title = str;
        price = num2;
        type = str1;
    }


    public static void main(String[] args) {

        Cdshop cd = new Cdshop();

        System.out.println("cd details are:");
        System.out.println("cd name is:" + cd.cid);
        System.out.println("cd title is:" + cd.title);
        System.out.println("cd price is:" + cd.price);
        System.out.println("cd type is:" + cd.type);

        Cdshop cd1 = new Cdshop(101, "Shape of water", 75, "Drama");

        System.out.println("cd details are:");
        System.out.println("cd name is:" + cd1.cid);
        System.out.println("cd title is:" + cd1.title);
        System.out.println("cd price is:" + cd1.price);
        System.out.println("cd type is:" + cd1.type);
    }
}

希望我能尽快得到答复,谢谢

在评论中,你说


一家CD店的老板希望保留一份CD目录。对于每张CD,他维护id、标题、价格和类型。通过为CD定义一个类,帮助所有者存储一些CD详细信息。还提供一种方法来了解到目前为止指定的CD总数(使用构造函数)

从“使用构造函数”这几个词来看,我想我知道这个任务打算让你做什么

您应该在
CdShop
类中添加一个字段,用于跟踪已创建的CD数量:

静态int cdCount=0

在两个构造函数中,添加以下行:

cdCount++;
这样做的目的是:每次调用构造函数时,都会创建一个
CdShop
对象,在构造函数中,我们向
cdCount
添加1,因此这将计算我们创建了多少张CD

main
方法的末尾,您可以打印出
cdCount
字段:

System.out.println(CdShop.cdCount);

我想你想知道构造函数被调用的次数。这可能就是你想要的

public class Cdshop {
 int cid;
 String title;
 int price;
 String type;
 static int count = 0;


 Cdshop(int cid, String title, int price, String type) {
  this.cid = cid;
  this.title = title;
  this.price = price;
  this.type = type;
  count++;
 }


 public static void main(String[] args) {


  Cdshop cd1 = new Cdshop(101, "Shape of water", 75, "Drama");

  System.out.println("cd details are:");
  System.out.println("cd name is:" + cd1.cid);
  System.out.println("cd title is:" + cd1.title);
  System.out.println("cd price is:" + cd1.price);
  System.out.println("cd type is:" + cd1.type);

  Cdshop cd2 = new Cdshop(102, "ABC", 45, "Sample");

  System.out.println("cd details are:");
  System.out.println("cd name is:" + cd1.cid);
  System.out.println("cd title is:" + cd1.title);
  System.out.println("cd price is:" + cd1.price);
  System.out.println("cd type is:" + cd1.type);
  System.out.println("Total cd's are::" + count);
 }
}

你的方法有点错误

你首先需要一张课堂CD。建议将成员变量设置为私有的,并且只能通过getter和setter访问

public class CD {
  private int id;
  private int price;
  private String title;
  private String type;

  // constructor
  public CD (int id, int price, String title, String type){
      this.id = id;
      this.price = price;
      this.title = title;
      this.type = type;
  }

  public int getId() {
     return id;
  }

  public void setId(int id) {
     this.id = id;
  }

  public int getPrice() {
     return price;
  }

  public void setPrice(int price) {
     this.price = price;
  }

  public String getType() {
     return type;
  }

  public void setType(int type) {
     this.type = type;
  }

  public String getTitle() {
     return title;
  }

  public void setTitle(int title) {
     this.title = title;
  }

}
有了它,您可以按照如下方式构建您的CDShop类(只是一个建议)


什么是Cdshop()?它的语法正确吗?Cdshop不应该维护CD列表吗?如果cd商店只能存储1张cd,那么总数将始终为1张,您能解释一下您的逻辑吗?“cd商店的所有者希望维护其cd的目录。对于每一张cd,他维护id、标题、价格和类型。通过定义cd的类来帮助所有者存储一些cd详细信息。同时提供一种方法,以了解到目前为止指定的cd总数(使用构造器)“.我已经试过回答上面的问题了非常感谢先生…!非常感谢先生…!非常感谢先生。。。!
public class CDShop {
    private List<CD> cdList = new ArrayList<CD>();

    public void addCD(CD cd) {
       cdList.add(cd);
    }

    public int getCDCount {
       return cdList.size();
    }
}
 CDShop cdShop = new CDShop();


 void storeCDs() {
   CD cd = new CD(1, 14, "Deep Blue", "Horror");
   cdShop.addCD(cd);

   cd = new CD(2, 12, "Matrix", "Action");
   cdShop.addCD(cd);

   cd = new CD(3, 24, "The Big Bang Theory", "Comedy");
   cdShop.addCD(cd);

 }

 void showCDCount() {
   System.out.println(cdList.getCDCount());
 }