我想计算一个 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/