要查找输入的项目数量?

时间:2018-04-12 06:39:41

标签: java

我是java的初学者,在下面的程序中我编写了一个cd商店的程序,重新调整商店中可用的cds,我想知道输入的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);
    }
}

希望我能尽快回答谢谢!

3 个答案:

答案 0 :(得分:1)

在评论中,你说,

  

CD店老板希望保留他的CD目录。对于每张CD,他保持身份证,头衔,价格,类型。通过为Cds定义类,帮助所有者存储一些CD细节。还提供了一种了解目前指定的CD总数的方法(使用构造函数)

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

您应该向CdShop类添加static字段,以跟踪已创建的CD数量:

static int cdCount = 0;

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

cdCount++;

这样做是这样的:每次调用构造函数时,都会创建一个CdShop对象,在构造函数中,我们将{1}添加1,这将计算我们创建的CD数量

cdCount方法结束时,您可以打印main字段:

cdCount

答案 1 :(得分:1)

你的做法有点不对劲。

您首先需要一张课程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课程如下(只是一个建议)

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());
 }

答案 2 :(得分:0)

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

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);
 }
}