编写一个名为PrintAsciiValue的Java程序来打印给定字符的ascii值。

如题所述

char的值即为其ascii码值..因此.代码如下

public class CharsSort {

// 排序,采用冒泡排序法
public static String sort(String str) {
char chs[] = str.toCharArray();
int size = chs.length;
char temp;
for (int i = 0; i < size; i++) {
for (int j = size - 1; j > i; j--) {
if (chs[j] < chs[j - 1]) {
temp = chs[j];
chs[j] = chs[j - 1];
chs[j - 1] = temp;
}
}
}
return new String(chs);
}

// 用做统计各类字符数目
public static void statistic(String str) {
int countOfNumber = 0;// 数字的数目
int countOfLeter = 0;// 字母的数目
int countOfSymbol = 0;// 符号的数目
char[] chs = str.toCharArray();
int size = chs.length;

// 判断字符类型,分类统计
for (int i = 0; i < size; i++) {
if ((chs[i] < 'z' && chs[i] > 'a')
|| (chs[i] < 'Z' && chs[i] > 'A')) {
countOfLeter++;
} else if (chs[i] < '9' && chs[i] > '0') {
countOfNumber++;
} else {
countOfSymbol++;
}
}

// 打印统计结果
System.err.println("包含字母:" + countOfLeter + "个");
System.err.println("包含数字:" + countOfNumber + "个");
System.err.println("包含其他符号:" + countOfSymbol + "个");
}

/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
// 测试结果
String str = "68765Shfsa*3219(6faylahjyAbo81av)tiA52N<6#6&0_84";
str = sort(str);// 字符串排序
System.err.println(str);// 打印结果
statistic(str);// 统计各类字符数目
}
}
温馨提示:答案为网友推荐,仅供参考

相关了解……

你可能感兴趣的内容

大家正在搜

本站内容来自于网友发表,不代表本站立场,仅表示其个人看法,不对其真实性、正确性、有效性作任何的担保
相关事宜请发邮件给我们
© 非常风气网