단어와 코드는 다음과 같습니다 :자바 카운트 문자열
package exercises;
import java.util.Scanner;
public class count {
public static int countwords(String str){
int count=0;
String space="";
String[] words=str.split(space);
for(String word:words){
if(word.trim().length()>0){
count++;
}
}
return count;
}
public static void main(String[] args) {
// TODO Auto-generated method stub
System.out.println("Enter the string");
Scanner input=new Scanner(System.in);
String s=input.nextLine();
System.out.println(countwords(s));
}
}
과 연습을하고 내가이
package exercises;
import java.util.Scanner;
public class count {
public static int countwords(String str){
int count=0;
String space="";
String[] words=str.split(space);
for(String word:words){
if(word.trim().length()>0){
count++;
}
}
return count;
}
public static void main(String[] args) {
// TODO Auto-generated method stub
System.out.println("Enter the string");
Scanner input=new Scanner(System.in);
String s=input.nextLine();
System.out.println(countwords(s));
}
}
로 다시 한 번이 코드를 작성
나는이 코드에 대해 다른 코드의 출력이 왜 다른지 궁금합니다. 코드를 한 줄씩 확인했지만이 두 코드의 출력이 다른 이유는 무엇입니까? 누구든지 도와주십시오
("") '... 당신이 (오히려 공간보다 빈 문자열을 분할 할 이유 또는 공백)? –