簡體   English   中英

如何根據各種輸入字段搜索數據

[英]how to search data based on various input fields

我有一組包含 3 個字段的數據,例如mobile_brandpricerating 現在有人帶着mobile_brand和他的預算(即他的價格范圍)。 我必須以最佳評級返回適合客戶價格范圍品牌的價值。 例如
視窗 200 100
視窗 100 52
安卓 300 9
ios 200 99
視窗 452 50
現在 customer1 有以下要求 mobile_type 和他的范圍
視窗 500
這給他留下了 3 個選項,但選項 1 的評分最高,所以應該退回。 我只是在摸索要使用什么數據結構以及如何比較。 我嘗試使用 map 但這不是解決方案,因為沒有唯一的字段來制作密鑰
注意:有超過 3 個字段,但為簡單起見,我只保留了 3 個字段。

我會去做一個簡單的多類事情。

public class Computer{
    public String type;
    public int price;
    public int rating;
    public Computer(String type, int price, int rating){ this.type=type; this.price=price; this.rating=rating;
}

您也許可以通過將評級除以價格來賺大錢。 顯然,您可以對此進行一些修補。

//elsewhere in a runner class
Computer mostEfficient=null;
double bestEfficiency=0;
for(int i=0;i<computers.length;i++){//where computers is an array of "Computer"s
    if(computers[i].type.Equals(typeWeAreLookingFor){
        double newEfficiency=(computers[i].rating+50)/((double)(computers[i].price));
        if(newEfficiency>bestEfficiency){
            mostEfficient=computers[i];
            bestEfficiency=newEfficiency;
        }
    }
} 

如果您需要知道如何從文本文件或其他內容中讀取這些字段,請告訴我

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM