gpt4 book ai didi

java - 计算字符串数组的每个字符串中的反斜杠

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

我想计算一个 url 数组中反斜杠“/”出现了多少次

并打印出每个url的值

这就是我现在在java中所拥有的

public class Main
{
public static void main (String[]args){
int count = 0;
String [] array = {"https://www.hello.com/app/login/","https://www.hello.com/sam/"
};
for(int i =0;i<array.length;i++){


for(int z = 0;z<array[i].length();z++){

if(array[i].charAt(z) == '/')
{
count++;

}


}
System.out.println(count);



}



}

}

但它打印了我们的 5 和 9 ,数字 9 代表数组中反斜杠的总数,我想要第二个网址的反斜杠数。不是总体数字

最佳答案

内部 for 循环完成后,必须将计数重置为 0:

public class Main
{
public static void main (String[]args){
int count = 0;
String [] array = {"https://www.hello.com/app/login/","https://www.hello.com/sam/"};
for(int i =0;i<array.length;i++){

for(int z = 0;z<array[i].length();z++){
if(array[i].charAt(z) == '/')
{
count++;
}
}
System.out.println(count);
count = 0; // reset the counter to 0 here
}
}
}

关于java - 计算字符串数组的每个字符串中的反斜杠,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58385564/

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