gpt4 book ai didi

java - 如何为由分隔符分割的循环添加条件?

转载 作者:行者123 更新时间:2023-12-01 09:26:54 25 4
gpt4 key购买 nike

   1 condition:

String str = "2d5";
String[] temp;
String delimiter = "d";
temp = str.split(delimiter);
String a=temp[0];
String b=temp[1];
Integer q,w,e;
q=Integer.parseInt(a);
w=Integer.parseInt(b);
e=q*w;
for(int i =q; i <=e ; i++){ //values will be (2,3.4,5,6,7,8,9,10)
data.add(new Card(i));} //data is an arraylist and Card is a class, here values will be stored in an arraylist)

2nd Condition:

str = ""2d5-2;
delimiter = "d|\\-";
temp = str.split(delimiter);
String a=temp[0];
String b=temp[1];
String c=temp[2];
q=Integer.parseInt(a);
w=Integer.parseInt(b);
r=Integer.parseInt(c);
for(int i =0; i < r ; i++){
data1.add(data.get(0)); // data1 is another arraylist
data.remove(0); // values will be removed from arraylist stating from index 0 i.e (4,5,6,7,8,9,10)

}

这里我实际上想做的是,每当我用“d”分隔符分割字符串时,我希望第一个条件运行,同样当我用“-”分隔符分割字符串时,我希望第二个条件在同一个类中运行。

最佳答案

您是否要求以下内容:

String [] temp = str.split("-");
if (temp.length == 1) // That is, "2d5" format
{
delimiter = "d";
temp = str.split(delimiter);
doCondition1(...);
}
else
{
delimiter = "d|\\-";
temp = str.split(delimiter);
doCondition2();
}

关于java - 如何为由分隔符分割的循环添加条件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39760949/

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