sinat_38214562
女青年与西西
2017-11-18 11:38

java判断char类型是否是空格

  • java
  • char
import java.util.Scanner;

public class B1057 {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        String input = in.nextLine();
        char basech1 = 'A';
        char basech2 = 'a';
        int sum = 0;
        for (int i = 0; i < input.length(); i++)
        {
            char ch = input.charAt(i);
            if ( ch != '\0' && ch != '(' && ch != ')')
            {
                if ((int)ch > 90)
                {
                    sum = sum + (ch - basech2);
                }
                else
                {
                    sum = sum + (ch - basech1);                 
                }
            }           
        }

        int []base2 = new int[32];
        int i = 0;
        while (sum != 0)
        {
            base2[i++] = sum % 2;  
            sum = sum / 2;          
        }

        for (int j = base2.length - 1; j >= 0; j--)
        {
            System.out.print(j);        
        }

        in.close();
    }

}


输入 PAT (Basic), debug看测试i = 3时,ch = str.CharAt(i) ,判断是不是空格,我用的
是'\0', if ( ch != '\0' && ch != '(' && ch != ')'), 还是进入了循环内,想问该怎么改?

  • 点赞
  • 回答
  • 收藏
  • 复制链接分享

2条回答