水题,需要注意需要转化为byte数组处理,因为一个汉字占两位,所以最后的结果要除2,附上AC代码。
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int n = in.nextInt();
in.nextLine();
for(int i = 0; i<n; i++){
String text = in.nextLine();
int cnt = 0;
byte[] bytes = text.getBytes();
for(int j = 0; j<bytes.length; j++){
if(bytes[j] < 0) cnt++;
}
System.out.println(cnt/2);
}
}
}