我的编程代码中缺少折扣程序代码

  • 本文关键字:代码 程序 编程 java
  • 更新时间 :
  • 英文 :


我认为一个代码丢失或不起作用,因为如果数字超过5,应该有折扣。例如,一本小说被借了6天,所以租金应该是8250。但不起作用,因为租金是9000。

这是我的程序代码:

**import java.util.Scanner;
class BookRentalShop 
{
public static void main(String[] args) 
{
Scanner sc = new Scanner(System.in);
System.out.print("Enter the number of data you want to see: ");
int inputnum = sc.nextInt();
sc.nextLine();
System.out.println("");
String[] Name = new String[inputnum];
String[] Bookname = new String[inputnum];
String[] AuthorName = new String[inputnum];
String[] Booktype = new String[inputnum];
int[] NumbersofDaysBorrowed = new int[inputnum];
int[] RentalCharges = new int[inputnum];
String[] Types = {"Cartoon","Magazine", "Short story", "Long story", "Journal", "Novel", "Encyclopedia"};
int[] count = new int[7];

for (int d = 0; d < inputnum; d = d + 1) 
{
System.out.println("Enter the name of the person:");
Name[d] = sc.nextLine();
System.out.println("Enter the bookname:");
Bookname[d] = sc.nextLine();
System.out.println("Enter the author's name:");
AuthorName[d] = sc.nextLine();
System.out.println("Enter the book type:");
Booktype[d] = sc.nextLine();
for (int k = 0; k < 7; k++)
{
if (Booktype[d].equals(Types[k]))
{
count[k]++; 
}
}
System.out.println("Enter the number of days that the book had been borrowed:");
NumbersofDaysBorrowed[d] = sc.nextInt();
sc.nextLine();
if (Booktype[d].equalsIgnoreCase("Cartoon")) 
{
RentalCharges[d] = NumbersofDaysBorrowed[d] * 500;
} else if (Booktype[d].equalsIgnoreCase("Magazine")) 
{
RentalCharges[d] = NumbersofDaysBorrowed[d] * 1000;
} else if (Booktype[d].equalsIgnoreCase("Short story")) 
{
RentalCharges[d] = NumbersofDaysBorrowed[d] * 500;
} else if (Booktype[d].equalsIgnoreCase("Long story")) 
{
RentalCharges[d] = NumbersofDaysBorrowed[d] * 1500;
} else if (Booktype[d].equalsIgnoreCase("Journal")) 
{
RentalCharges[d] = NumbersofDaysBorrowed[d] * 350;
} else if (Booktype[d].equalsIgnoreCase("Novel")) 
{
RentalCharges[d] = NumbersofDaysBorrowed[d] * 1500;
} else {
RentalCharges[d] = NumbersofDaysBorrowed[d] * 2500;
}
}
System.out.printf("%s %20s %20s %20s %20s %20s %20sn", "No", "Name", "Bookname", "AuthorName", "Booktype", "Numbers of Days Borrowed", "Rental Charges");
for (int d = 0; d < inputnum; d = d + 1) 
{
int num = d + 1;
System.out.printf("%s %20s %20s %20s %20s %20d %20dn", num, Name[d], Bookname[d], AuthorName[d], Booktype[d], NumbersofDaysBorrowed[d], RentalCharges[d]);
}
String again = "Yes";
String exist = "No";
while (again.equals("Yes")) {
exist = "No";
System.out.println("enter search name");
String searchname = sc.nextLine();
for (int d = 0; d < inputnum; d = d + 1) {
if (searchname.equals(Name[d])) {
System.out.println("Name           : " + Name[d]);
System.out.println("Bookname       : " + Bookname[d]);
System.out.println("Number of Days : " + NumbersofDaysBorrowed[d]);
exist = "Yes";
}
}
if (exist.equals("No")) {
System.out.println("The search name is not found");
}
System.out.println("Do you want to search again? (Yes,No) ");
again = sc.nextLine();
}
int max = count[0];
for (int d = 0; d < 7; d = d + 1)
{
for (int k =  d + 1; k < 7; k = k + 1)
{
if (count[k] > count[d]) 
{
max = count[k];
}
else {
max = count[d];
}
}
}
System.out.println("");
System.out.println("The most rented book is: " + max);
System.out.println("");
}
}

项目结果:

No                 Name             Bookname           AuthorName             Booktype Numbers of Days Borrowed       Rental Charges
1               ghjghj              hjghjgh                hfghg              Journal                    3                 1050
2           hghjhgjhgj           uyiuyjghjg           ghytghghjg                Novel                    6                 9000
3               bcvnvn              dasdasd               weqwew              Cartoon                    5                 2500*

如果有人能帮我找到这个问题,我将不胜感激。

我建议如下解决方案:

这是你的代码片段:

System.out.println("Enter the number of days that the book had been borrowed:");
NumbersofDaysBorrowed[d] = sc.nextInt();
sc.nextLine();
if (Booktype[d].equalsIgnoreCase("Cartoon")) {
RentalCharges[d] = NumbersofDaysBorrowed[d] * 500;
} else if (Booktype[d].equalsIgnoreCase("Magazine")) {
RentalCharges[d] = NumbersofDaysBorrowed[d] * 1000;
} else if (Booktype[d].equalsIgnoreCase("Short story")) {
RentalCharges[d] = NumbersofDaysBorrowed[d] * 500;
} else if (Booktype[d].equalsIgnoreCase("Long story")) {
RentalCharges[d] = NumbersofDaysBorrowed[d] * 1500;
} else if (Booktype[d].equalsIgnoreCase("Journal")) {
RentalCharges[d] = NumbersofDaysBorrowed[d] * 350;
} else if (Booktype[d].equalsIgnoreCase("Novel")) {
RentalCharges[d] = NumbersofDaysBorrowed[d] * 1500;
} else {
RentalCharges[d] = NumbersofDaysBorrowed[d] * 2500;
}

在这个代码下尝试添加这个。它取代了数组中计算的图书成本减去折扣,例如当天数超过5天时,折扣为750。你可以随心所欲地更改

if(NumbersofDaysBorrowed[d] > 5)
{
int bookCost = RentalCharges[d] / NumbersofDaysBorrowed[d];
RentalCharges[d] = (5 * bookCost) + ((NumbersofDaysBorrowed[d] - 5) * (bookCost / 2)); // DISCOUNT
}

最新更新