지금까지 길이를 얻는 데 아무런 문제가 없지만 글자의 숫자를 인식하는 것은 어렵습니다. 도움이 필요하시면 도움을 받으십시오. 새 코드가 도움이되었습니다. 문자열의 요소를 세는 것은 주소 예제와 같이 입력 된 숫자를 계산하지 않습니다. San Pedro St Philippines ..... 산 페드로 세인트 필리핀 만 계산합니다 .........입력 된 문자열에서 숫자와 문자를 인식했습니다.
import java.util.Scanner;
public class Exercise3
{
public static void main(String [] args)
{
Scanner scan= new Scanner(System.in);
System.out.println("Enter String:");
String s=scan.nextLine();
s = s.replace(" ","");
System.out.println("Total of Elements is: " + s.length());
int nDigits =0,nLetters =0,sum =0;
for(int i =0;i<s.length();i++)
{
Character ch = s.charAt(i);
if(Character.isDigit(ch)){
nDigits++;
sum += Integer.parseInt(ch.toString());
}
else if (Character.isLetter(ch)){
nLetters++;
}
}
System.out.println("The sum of numbers in the string: " + sum);
}
}
}
_if 문자가 숫자 인 경우 전체 문자열을 숫자 합계로 _ _... 생각하면됩니다. – Ben
팁을 주셔서 감사합니다. Ben – user3820685