如何在java中读取空格分隔的字符?我尝试了各种方法但没有成功。我想计算一下' O'减去' I'
的数量这是我的代码:
package RegExp;
import java.io.*;
import java.util.Scanner;
public class CounterO {
public static void main(String[] args) throws IOException {
Scanner sc = new Scanner(System.in);
char c;
int count = 0;
while (sc.hasNext()) {
c = sc.nextLine().charAt(0);
if (c == 'O')
count++;
else if (c == 'I')
count--;
}
sc.close();
System.out.println(count);
}
}
// INPUT:A B A O I O O O O O O O O I I O I I