java - 如何在图书馆系统的编程代码中编写代码或显示最常租借的书籍类型?
问题描述
我想我在我的编程代码中写了一个错误的代码,因为结果显示的是最租借的图书类型的数字,而不是图书类型的名称。例如,一个人借了一本小说书类型,另一个人也借了一本小说书类型。因此,当我运行该程序时,它会显示一个数据列表,其中包含 2 个人借用相同的图书类型,即 Novel,然后它显示“最常租借的图书类型是:2”。什么时候应该显示“租借最多的书类型是:小说”
这是我的编程Java代码:
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;
}
if(NumbersofDaysBorrowed[d] > 5)
{
int bookCost = RentalCharges[d] / NumbersofDaysBorrowed[d];
RentalCharges[d] = (5 * bookCost) + ((NumbersofDaysBorrowed[d] - 5) * (bookCost / 2));
}
}
System.out.printf("%s %20s %20s %20s %20s %20s %20s\n", "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 %20d\n", 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 the 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 Borrowed : " + NumbersofDaysBorrowed[d]);
exist = "Yes";
}
}
if (exist.equals("No")) {
System.out.println("The search name requested 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 booktype is: " + max);
System.out.println("");
}
}*
这是代码的结果:
No Name Bookname AuthorName Booktype Numbers of Days Borrowed Rental Charges
1 J Grere bvcnvnb Journal 3 1050
2 K wqerwr xczzzx Novel 6 8250
3 opoipo kkghjhjgh bvcbcvbc Cartoon 5 2500
4 Q erwytiu ghfghgfd Magazine 7 6000
5 D sdsafhgjhgjk vvbbnbn,nbvc Novel 6 8250
enter the search name
No
The search name requested is not found
Do you want to search again? (Yes,No)
No
The most rented booktype is: 2
我用靠近我的代码末尾的星号 (*) 指出了我认为代码错误的地方。
解决方案
您正在打印int
值而不是字符串。尝试打印出来Booktype[max]
System.out.println("The most rented booktype is: " + Booktype[max]);
推荐阅读
- asynchronous - 为什么 Data stax 企业 java 驱动程序 Async 方法返回 Guava 的 ListenableFutures 而不是 Java 的 CompletableFutures?
- xml - Powershell XML 文件到表格视图
- android - Vue 路由器不适用于某些 Android。有解决方法吗?
- r - if (num < 0) { 中的错误:需要 TRUE/FALSE 的缺失值
- python - Python pip 安装 appkit
- magento2 - magento 2.3 | bin/magento setup:di:compile .. 停止 @ 14%
- java - 扫描仪被跳过
- reactjs - 材质 UI 列表 - 单独的 OnClick 以展开
- javascript - 如何自动执行 JavaScript 架构/依赖规则?
- asp.net-core - Body 标签上的条件 HTML 标签