gpt4 book ai didi

java - 创建一个java方法查找费CD类

转载 作者:行者123 更新时间:2023-12-01 23:27:08 26 4
gpt4 key购买 nike

我已经创建了一个java程序,允许从 Collection 中借用CD,但是我现在需要创建一个方法find Fee(),该方法对借用1-20次的CD收取10便士的费用,对借用21次的CD收取20便士的费用-30次,对于借阅超过30次的CD收取购买成本的5%,到目前为止我尝试的代码:

public void borrow(String personBorrowed)
{
person = personBorrowed;
inStock = false;
timesBorrowed = timesBorrowed + 1;
}
public void main(String[] args)
{ if (timesborrowed >= 1-19) { cost = '+10p'; }
else if (timesborrowed >= 1-29) { cost = '20p'; }
else if (timesborrowed >= 30) { cost = '+ 10.00/5= 2.00'; }
System.out.println("Cost = " + cost); } }

(以上代码来自底行)

我的代码:

public class CD
{
// instance variables - replace the example below with your own
private String title;
private String artist;
private int noOfTracks;
private double cost;
private boolean inStock;
private String person;
private int timesBorrowed;
private boolean returnCD;

/**
* Constructor for objects of class CD
*/
public CD(String newTitle, String newArtist,int newNoOfTracks,double newCost)
{
// initialise instance variables
title = newTitle;
artist = newArtist;
noOfTracks = newNoOfTracks;
cost = newCost;
inStock = true;
person = null;
timesBorrowed = 0;
}


/**
* Default Constructor for Testing
*/
public CD()
{
// initialise instance variables
title = "Blue Print";
artist = "Jay Z";
noOfTracks = 15;
cost = 10.00;
inStock = true;
person = null;
timesBorrowed = 0;
}



/**
* An example of a method - replace this comment with your own
*/

public String getTitle()
{
return title;
}


/**
* An example of a method - replace this comment with your own
*/

public String getArtist()
{
return artist;
}


/**
* An example of a method - replace this comment with your own
*/

public int getNoOfTracks()
{
return noOfTracks;
}


/**
* An example of a method - replace this comment with your own
*/

public double getCost()
{
return cost;
}


/**
* An example of a method - replace this comment with your own
*/

public void printDetails()
{
System.out.println("Title: " + title);
System.out.println(" ");
System.out.println("Artist: " + artist);
System.out.println(" ");
System.out.println("Number of Tracks: " + noOfTracks);
System.out.println(" ");
System.out.println("Cost: " + cost);
}


/**
* An example of a method - replace this comment with your own
*/

public void borrow(String personBorrowed)
{
person = personBorrowed;
inStock = false;
timesBorrowed = timesBorrowed + 1;
}
public void main(String[] args)
{ if (timesborrowed >= 1-19) { cost = '+10p'; }
else if (timesborrowed >= 1-29) { cost = '20p'; }
else if (timesborrowed >= 30) { cost = '+ 10.00/5= 2.00'; }
System.out.println("Cost = " + cost); } }
}

任何答案或答复和帮助将不胜感激,因为我真的很困惑并且无法弄清楚这一点。

最佳答案

以下内容毫无意义:

 { if (timesborrowed >= 1-19) { cost = '+10p'; } 
else if (timesborrowed >= 1-29) { cost = '20p'; }
else if (timesborrowed >= 30) { cost = '+ 10.00/5= 2.00'; }

用途:

if(timesborrowed<20) {cost+=0.10;}
else if (timesborrowed <30) {cost +=-.20;}
else {cost +=2;}

我假设 p 指的是便士或便士,因此是面额的 1/100,

关于java - 创建一个java方法查找费CD类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19826314/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com