谁能使用Java为这个问题提供解决方案吗? [关闭]

问题描述 投票:-3回答:1

[创建具有以下属性的课本:

  • id-int
  • 标题字符串
  • 作者字符串
  • 双倍价格

根据需要编写getter,setter和参数化构造函数。

使用主方法创建类解决方案。

在解决方案类中实施静态方法-searchTitle。

此方法将String值的一个参数与其他参数一起作为Book对象的数组。它将返回书本数组,其中String值参数出现在title属性中(不区分大小写)。

此方法应从main方法中调用,并以升序显示返回对象的ID。在调用此方法之前,请使用Scanner对象读取上述顺序中引用属性的四个Book对象的值。

下一步读取值搜索参数。

接下来调用该方法并显示结果。

样本输入:

1
hello world
aaa writer
50
2
World cup
bbb writer
55
3
Planet earth
ccc writer
45
4
India's history
ddd writer
40
WORLD

输出:

1
2

代码

import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;
class Book
{
    int id;
    String title,author;
    double price;
    Book(int id, String title, String author, double price)
    {
        this.id=id;
        this.title=title;
        this.author=author;
        this.price=price;
    }
    public int getId()
    {
        return this.id;
    }
    public String getTitle()
    {
        return this.title;
    }
    public String getAuthor()
    {
        return this.author;
    }
    public double getPrice()
    {
        return this.price;
    }
    public void setId(int id)
    {
        this.id=id;
    }
    public void setTitle(String title)
    {
        this.title=title;
    }
    public void setAuthor(String author)
    {
        this.author=author;
    }
    public void setPrice(double price)
    {
        this.price=price;
    }
}
class Solution
{
    public static Book[] searchTitle(Book[] b, String inputTitle)
    {
        Book[] Arr=new Book[4];
        for(int k=0;k<b.length;k++)
        {
         if(b[k].getTitle().toLowerCase().equals(inputTitle.toLowerCase()))
            {
                Arr[k]=b[k];
            }
        }  
        return Arr;
    }
    public static void main(String []args) throws Exception
    {
        Book[] b1=new Book[4];
        Scanner sc=new Scanner(System.in);
        for(int i=0;i<4;i++)
        {
            int id=sc.nextInt();
            sc.nextLine();
            String title=sc.nextLine();
            String author=sc.nextLine();
            double price=sc.nextDouble();
            sc.nextLine();
            b1[i]=new Book(id,title,author,price);
        }
        String inputTitle=sc.nextLine();
        Book[]result=searchTitle(b1, inputTitle);
        for(int i=0;i<result.length;i++)
        {
            for(int j=0;j<result.length-1;j++)
            {
                if(b1[j].id>b1[j+1].id)
                {
                    Book temp=b1[j];
                    b1[j]=b1[j+1];
                    b1[j+1]=temp;
                }
            }
        }
        for(int i=0;i<result.length;i++)
        {
                if(result[i]!=null)
                System.out.println(result[i].getId());
        }
    }
}
java arrays search
1个回答
-2
投票

您首先要创建一个Book Class,然后将其用于放置到数组中。为此,您需要查找如何编写自定义类(https://www.learnhowtoprogram.com/java/java-basics-9a4b2b2a-6de4-44b5-9f35-0e506177d73b/declaring-custom-classes-in-java)。

一旦创建,您就想要创建一个像method-searchTitle(String bookName,Book [] bookArr)之类的方法。为了使其不区分大小写,您可以在bookName和bookArr书本对象之间进行.toLowerCase()比较。

然后您可以将生成的ID放入数组中并对其进行排序,然后使用System.out.println()将其打印出来。

然后其余的内容在使用扫描仪时应能自我解释。

© www.soinside.com 2019 - 2024. All rights reserved.