1. 程式人生 > >修改字串[email protected]&b&c....中b的值並輸出改變b值後的字串

修改字串[email protected]&b&c....中b的值並輸出改變b值後的字串

修改字串a&b&[email protected]&b&c…中b的值並輸出改變b值後的字串

需求:車輛掃描PDA入庫,每次入庫的時候都要修改入庫單的待入庫數量,入庫單中只有一個a&b:&@a&b&c a(倉位id)b(要求入倉數量)c(其它)的資料 解決方法:

/**
 * @author jiangruliang
 * @date 2018年9月27日11:43:11
 * @Description 貨物入倉 修改待入倉數量 待入倉字串格式flag a:b:[email protected]:b:c a(倉位id)b(要求入倉數量)c(其它) ,此次入倉數量inStorageNum
 */
public class Test {

  public static void main(String[] args) {
    String waitStorageNum = "1&1&
[email protected]
&2&[email protected]&2&[email protected]&2&1".replaceAll("@", "&"); double inStorageNum = 6.5; String[] strings = waitStorageNum.split("&"); StringBuilder b = new StringBuilder(); for (int i = 0; i < strings.length; i++) { //(i + 2) % 3 為要求入倉數量 if ((i + 2) % 3 == 0) { if (Double.parseDouble((strings[i])) > inStorageNum && inStorageNum >= 0) { b.append(String.valueOf(Double.parseDouble((strings[i])) - inStorageNum)).append("&"); } else if ((Double.parseDouble((strings[i])) <= inStorageNum && inStorageNum >= 0)) { b.append("0").append("&"); } else { b.append(strings[i]).append("&"); } inStorageNum = inStorageNum - Double.parseDouble((strings[i])); } //(i + 1) % 3 為(其它)並排除最後一位 if ((i + 1) % 3 == 0 && i != strings.length - 1) { b.append(strings[i]).append("@"); } //(i + 1) % 3 為倉位id if (i % 3 == 0) { b.append(strings[i]).append("&"); } //最後一位 if (i == strings.length - 1) { b.append(strings[i]); } } System.out.println(b); } }

上述程式碼輸出結果 在這裡插入圖片描述