Java 判断字符串中()的个数是否相等

import java.util.Scanner;
class Zonghexunlian1_7{
	public static void main(String []args){
		System.out.println("输入一个字符串 判断( )的个数是否相等");
		Scanner sc=new Scanner(System.in);
		String s=sc.nextLine();
		if(count(s,'(')==count(s,')'))//比较左右括号的出现次数
			System.out.println("YES");
		else
			System.out.println("NO");
	}
	public static int count(String s,char c){
		int x=0;
		for(int k=0;k<s.length();k++)
			if(s.charAt(k)==c)
				x++;
			return x;
	}
}

猜你喜欢

转载自blog.csdn.net/shihao9895/article/details/83959044