第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何顯示以特定數(shù)字開頭的所有值

如何顯示以特定數(shù)字開頭的所有值

哈士奇WWW 2023-05-17 17:16:40
我正在創(chuàng)建一個搜索來過濾一些從 100 到 600 的整數(shù)值。這些值是Http Status Codes,所以我只想過濾它們。搜索的工作方式如下:用戶輸入搜索值,例如2并單擊搜索結(jié)果將是從200 到 299的所有值(因此所有值都以 2 開頭)。用戶輸入一個值,例如20,然后單擊搜索結(jié)果將是從200 到 209的所有值(因此所有值都以 20 開頭)。用戶輸入一個值,例如52,然后單擊搜索結(jié)果將是從520 到 529 的所有值(因此所有值都以 52 開頭)我寫了一些非常多余的代碼,但基本上解釋了它應該如何工作:? ?public void getHttpStatus(Integer httpStatus){? ? ? ? ? ? if(httpStatus.equals(1)){? ? ? ? ? ? ? ? messageRepository.findByHttpStatusBetween(100, 199);? ? ? ? ? ? }? ? ? ? ? ? else if(httpStatus.equals(2)){? ? ? ? ? ? ? ? messageRepository.findByHttpStatusBetween(200, 299);? ? ? ? ? ? }? ? ? ? ? ? else if(httpStatus.equals(3)){? ? ? ? ? ? ? ? messageRepository.findByHttpStatusBetween(300, 399);? ? ? ? ? ? }? ? ? ? ? ? else if(httpStatus.equals(4)){? ? ? ? ? ? ? ? messageRepository.findByHttpStatusBetween(400, 499);? ? ? ? ? ? }? ? ? ? ? ? else if(httpStatus.equals(5)){? ? ? ? ? ? ? ? messageRepository.findByHttpStatusBetween(500, 599);? ? ? ? ? ? }? ? ? ? ? ? else if(httpStatus.equals(10)){? ? ? ? ? ? ? ? messageRepository.findByHttpStatusBetween(100, 109);? ? ? ? ? ? }? ? ? ? ? ? else if(httpStatus.equals(20)){? ? ? ? ? ? ? ? messageRepository.findByHttpStatusBetween(200, 209);? ? ? ? ? ? }? ? ? ? ? ? else if(httpStatus.equals(30)){? ? ? ? ? ? ? ? messageRepository.findByHttpStatusBetween(300, 309);? ? ? ? ? ? }? ? ? ? ? ? else if(httpStatus.equals(40)){? ? ? ? ? ? ? ? messageRepository.findByHttpStatusBetween(400, 409);? ? ? ? ? ? }? ? ? ? ? ? else if(httpStatus.equals(50)){? ? ? ? ? ? ? ? messageRepository.findByHttpStatusBetween(500, 509);? ? ? ? ? ? }? ? ? ? ? ? else if(httpStatus.equals(21)){? ? ? ? ? ? ? ? messageRepository.findByHttpStatusBetween(210, 219);? ? ? ? ? ? }? ? ? ? ? ? ...? ? ? ? }有沒有更簡單的方法來做到這一點?或者是否有任何內(nèi)置的 spring 方法可以自動執(zhí)行此操作?我將不勝感激任何建議。
查看完整描述

4 回答

?
交互式愛情

TA貢獻1712條經(jīng)驗 獲得超3個贊

假設您有一個包含所有狀態(tài)代碼的列表,您可以使用流過濾器,即


List<Integer> httpCodes;

String prefix = "5"

List<Integer> filteredResults = httpCodes.stream().filter(value -> value.toString().startsWith(prefix)).collect(Collectors.toList());



查看完整回答
反對 回復 2023-05-17
?
長風秋雁

TA貢獻1757條經(jīng)驗 獲得超7個贊

我可能會這樣做:


public void getHttpStatus(Integer httpStatus){

 int numberOfDigits = (int) (Math.log10(number) + 1);

 int minStatus = httpStatus * ((int) Math.pow(10, 3 - numberOfDigits));

 int maxStatus = (httpStatus + 1) * ((int) Math.pow(10, 3 - numberOfDigits)) - 1

 messageRepository.findByHttpStatusBetween(minStatus,maxStatus)


}

或者你可以這樣做,


String status=  httpStatus.toString();

String startIndex = status;

String endIndex = status;


if ( status.length() == 1 )

{

    startIndex = status + "00";

    endIndex = status + "99";

}

else if ( status.length() == 2 )

{

    startIndex = status + "0";

    endIndex = status + "9";

}

int sIndex = Integer.parseInt( startIndex );

int eIndex = Integer.parseInt( endIndex );


messageRepository.findByHttpStatusBetween(sIndex, eIndex);


查看完整回答
反對 回復 2023-05-17
?
動漫人物

TA貢獻1815條經(jīng)驗 獲得超10個贊

將代碼存儲為字符串,編寫自定義查詢以查找“LIKE '50%'”等條目(LIKE '$1%')。

查看完整回答
反對 回復 2023-05-17
?
POPMUISE

TA貢獻1765條經(jīng)驗 獲得超5個贊

public class Range {

    public int lb;

    public int ub;


    public Range(int lb, int ub) {

        this.lb = lb;

        this.ub = ub;

    }


    public Range(int statusCode) {

        int length = (int) (Math.log10(statusCode) + 1);

        if (length == 0 || length > 2) {

            throw new RuntimeException("Cant parse status code of invald length: " + length);

        }

        if (length == 1) {

            this.lb = statusCode * 100;

            this.ub = ((statusCode + 1) * 100) - 1;

        } else {

            this.lb = statusCode * 10;

            this.ub = ((statusCode + 1) * 10) - 1;

        }

    }

}



public class Main {

    public static void main(String[] args) {

        Range r1 = new Range(52);

        Range r2 = new Range(2);

        Range r3 = new Range(20);

        System.out.println("Lowerbound: " + r1.lb);

        System.out.println("Upperbound: " + r1.ub);

        System.out.println("Lowerbound: " + r2.lb);

        System.out.println("Upperbound: " + r2.ub);

        System.out.println("Lowerbound: " + r3.lb);

        System.out.println("Upperbound: " + r3.ub);

    }

}

輸出如下:


Lowerbound: 520

Upperbound: 529

Lowerbound: 200

Upperbound: 299

Lowerbound: 200

Upperbound: 209

不檢查特殊值 0。


然后你的函數(shù)可以重構(gòu)為:


public void getHttpStatus(Integer httpStatus){

        Range r = new Range(httpStatus.intValue());

        messageRepository.findByHttpStatusBetween(r.lb, r.ub);

}


查看完整回答
反對 回復 2023-05-17
  • 4 回答
  • 0 關(guān)注
  • 192 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學習伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號