JAVA屏幕输入一串数字字符串,输入后统计有多少个偶数数字和奇数数字。

package zuoye_3_25;
import java.util.Scanner;
public class Three {
	public static void main(String[] args) {
		Scanner s = new Scanner(System.in);
		System.out.println("请输入一个数字字符串!");
		String str=s.nextLine();
		int count1=0;
		int count2=0;
		for(int i=0;i<str.length();i++) {
			if(str.charAt(i)%2==0) {
				count1++;
			}else {
				count2++;
			}
		}
		System.out.println("有"+count1+"个偶数!"+"有"+count2+"个奇数!");
	}
}

charAt[i]取出字符串的值:
例如:
String str=“我爱你中国!”;
str.charAt[1]:爱
str.charAt[0]:我
str.charAt[2]:你

猜你喜欢

转载自blog.csdn.net/qq_42249896/article/details/88796014