gpt4 book ai didi

java - 循环遍历二维数组

转载 作者:太空宇宙 更新时间:2023-11-04 12:08:57 25 4
gpt4 key购买 nike

我想将两个字符串保存在二维字符串数组中,并进一步将其用于其他方法。

我的数组:

String[][] x = {{"Iron Ice Tea", "5"}, {"Clock Clock", "3"}, {"Shoe Telephone", "3"}};

我的方法:

  public static String getInsertedCustomerOrder(String[][] input) {
String query = "";
int x = 0;

for (String[] product : input) {
for (String count : product) {
if (x == 0) {
query = query + "((SELECT PRICE FROM PRODUCT WHERE NAME = '" + product + "') * " + count + " + ";
} else {
query = query + "(SELECT PRICE FROM PRODUCT WHERE NAME = '" + product + "') * " + count + " + ";
}
x++;
}
}

if (query != null && query.length() > 0 && query.charAt(query.length() - 2) == '+') {
query = query.substring(0, query.length() - 2);
}

query = query + ")";

return query;
}

输出为:

((从名称='[Ljava.lang.String;@1db9742'的产品中选择价格)*铁冰茶+(从名称='[Ljava.lang.String;@1db9742'的产品中选择价格)*5+(从名称='[Ljava.lang.String;@106d69c'的产品中选择价格)*时钟时钟+(选择来自产品的价格,其中名称 = '[Ljava.lang.String;@106d69c') * 3 + (从产品中选择价格,其中名称 = '[Ljava.lang.String;@52e922') * 鞋子 电话 + (从产品中选择价格,其中名称 = '[Ljava.lang.String;@52e922') * 3 )

而应该是:

((SELECT PRICE FROM PRODUCT WHERE NAME = 'Iron Ice Tea') * 5))

最佳答案

试试这个。

public static String getInsertedCustomerOrder(String[][] input) {
boolean first = true;
String query = "";
for (String[] product : input) {
if (first)
first = false;
else
query += " + ";
query += "(SELECT PRICE FROM PRODUCT WHERE NAME = '" + product[0] + "') * "
+ product[1];
}
query = "(" + query + ")";
return query;
}

还有

String[][] input = { { "Iron Ice Tea", "5" }, { "Clock Clock", "3" }, { "Shoe Telephone", "3" } };
System.out.println(getInsertedCustomerOrder(input));

结果:

((SELECT PRICE FROM PRODUCT WHERE NAME = 'Iron Ice Tea') * 5 + (SELECT PRICE FROM PRODUCT WHERE NAME = 'Clock Clock') * 3 + (SELECT PRICE FROM PRODUCT WHERE NAME = 'Shoe Telephone') * 3)

关于java - 循环遍历二维数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40061229/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com