用java程序生成助记码,即输入张三时,得到的结果是JS;获得名称的首字母大写
2017-07-03 13:56
323 查看
import java.lang.*;
public class GB2Alpha {
//字母Z使用了两个标签,这里有27个值
//i, u, v都不做声母, 跟随前面的字母
private char[] chartable =
{
'啊', '芭', '擦', '搭', '蛾', '发', '噶', '哈', '哈',
'击', '喀', '垃', '妈', '拿', '哦', '啪', '期', '然',
'撒', '塌', '塌', '塌', '挖', '昔', '压', '匝', '座'
};
private char[] alphatable =
{
'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I',
'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z'
};
private int[] table = new int[27];
//初始化
{
for (int i = 0; i < 27; ++i) {
table[i] = gbValue(chartable[i]);
}
}
public GB2Alpha() {
}
//主函数,输入字符,得到他的声母,
//英文字母返回对应的大写字母
//其他非简体汉字返回 '0'
public char Char2Alpha(char ch) {
if (ch >= 'a' && ch <= 'z')
return (char) (ch - 'a' + 'A');
if (ch >= 'A' && ch <= 'Z')
return ch;
int gb = gbValue(ch);
if (gb < table[0])
return '0';
int i;
for (i = 0; i < 26; ++i) {
if (match(i, gb))
break;
}
if (i >= 26)
return '0';
else
return alphatable[i];
}
//根据一个包含汉字的字符串返回一个汉字拼音首字母的字符串
public String String2Alpha(String SourceStr) {
String Result = "";
int StrLength = SourceStr.length();
int i;
try {
for (i = 0; i < StrLength; i++) {
Result += Char2Alpha(SourceStr.charAt(i));
}
} catch (Exception e) {
Result = "";
}
return Result;
}
private boolean match(int i, int gb) {
if (gb < table[i])
return false;
int j = i + 1;
//字母Z使用了两个标签
while (j < 26 && (table[j] == table[i]))
++j;
if (j == 26)
return gb <= table[j];
else
return gb < table[j];
}
//取出汉字的编码
private int gbValue(char ch) {
String str = new String();
str += ch;
try {
byte[] bytes = str.getBytes("GB2312");
if (bytes.length < 2)
return 0;
return (bytes[0] << 8 & 0xff00) + (bytes[1] &
0xff);
} catch (Exception e) {
return 0;
}
}
public static void main(String[] args) {
GB2Alpha obj1 = new GB2Alpha();
System.out.println(obj1.String2Alpha("测试:中华人民共和国!"));
System.out.println(obj1.String2Alpha("裴贺先"));
return;
}
}
public class GB2Alpha {
//字母Z使用了两个标签,这里有27个值
//i, u, v都不做声母, 跟随前面的字母
private char[] chartable =
{
'啊', '芭', '擦', '搭', '蛾', '发', '噶', '哈', '哈',
'击', '喀', '垃', '妈', '拿', '哦', '啪', '期', '然',
'撒', '塌', '塌', '塌', '挖', '昔', '压', '匝', '座'
};
private char[] alphatable =
{
'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I',
'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z'
};
private int[] table = new int[27];
//初始化
{
for (int i = 0; i < 27; ++i) {
table[i] = gbValue(chartable[i]);
}
}
public GB2Alpha() {
}
//主函数,输入字符,得到他的声母,
//英文字母返回对应的大写字母
//其他非简体汉字返回 '0'
public char Char2Alpha(char ch) {
if (ch >= 'a' && ch <= 'z')
return (char) (ch - 'a' + 'A');
if (ch >= 'A' && ch <= 'Z')
return ch;
int gb = gbValue(ch);
if (gb < table[0])
return '0';
int i;
for (i = 0; i < 26; ++i) {
if (match(i, gb))
break;
}
if (i >= 26)
return '0';
else
return alphatable[i];
}
//根据一个包含汉字的字符串返回一个汉字拼音首字母的字符串
public String String2Alpha(String SourceStr) {
String Result = "";
int StrLength = SourceStr.length();
int i;
try {
for (i = 0; i < StrLength; i++) {
Result += Char2Alpha(SourceStr.charAt(i));
}
} catch (Exception e) {
Result = "";
}
return Result;
}
private boolean match(int i, int gb) {
if (gb < table[i])
return false;
int j = i + 1;
//字母Z使用了两个标签
while (j < 26 && (table[j] == table[i]))
++j;
if (j == 26)
return gb <= table[j];
else
return gb < table[j];
}
//取出汉字的编码
private int gbValue(char ch) {
String str = new String();
str += ch;
try {
byte[] bytes = str.getBytes("GB2312");
if (bytes.length < 2)
return 0;
return (bytes[0] << 8 & 0xff00) + (bytes[1] &
0xff);
} catch (Exception e) {
return 0;
}
}
public static void main(String[] args) {
GB2Alpha obj1 = new GB2Alpha();
System.out.println(obj1.String2Alpha("测试:中华人民共和国!"));
System.out.println(obj1.String2Alpha("裴贺先"));
return;
}
}
相关文章推荐
- Java入门小程序,使用Java转换用户输入字母为大写如何避开中文?
- java程序,编写一个程序,输出一个字符串中的大写英文字母数,小写英文字母数及非英文字母数
- 串的处理 在实际的开发工作中,对字符串的处理是最常见的编程任务。本题目即是要求程序对用户输入的串进行处理。具体规则如下: 1. 把每个单词的首字母变为大写。 2. 把数字与字母之间用下划线字符(_)分
- java程序获得windows系统的一些参数(cmd指令运行返回结果)
- 编写一个Java应用程序。用户从键盘输入一个1—9999之间的数,程序将判断这个数是几位数, 并判断这个数是否是回文数。回文数是指将该数含有的数字逆序排列后得到的数和原数相同, 例如12121、3
- 编写IoDemo.java的Java应用程序,程序完成的功能是:首先读取text.txt文件内容,再通过键盘输入文件的名称为iodemo.txt,把text.txt的内容存入iodemo.txt
- 使用java简单实现根据输入的汉字,得到汉字的首字母
- 通过键盘输入一串小写字母(a~z)组成的字符串。请编写一个字符串过滤程序,若字符串中出现多个相同的字符,将非首次出现的字符过滤掉。 比如字符串“abacacde”过滤结果为“abcde”。
- Java 运行程序,并获得结果
- 笔试题——编写java程序:输入一个字符串,判断有几个英文字母,有几个数字,有几个其它的字符
- Java变量及类名称定义坑之第二个字母大写
- JS_Java_获得工程项目名称
- Java 文件统计:编写程序,统计英文文本文件中的字符数目和单词数目。程序运行时,输入要统计的文件的名称,程序处理后输出字符数目和单词数目
- Java小程序:输入字母和数字,输出时字母在前数字在后
- 1. 请用C#的面向对象语言 实现 一个计算器控制台程序,要求输入两个数和运算符号,得到结果
- 2. 编写一个Java应用程序,用户从键盘输入一个1~9999之间的数,程序将判断这个数是几位数,并判断这个数是否是回文数。回文数是指将该数含有的数字逆序排列后得到的数和原数相同,例如12121、32
- JAVA 输入一个字符串,将大写字母改成小写,小写字母改成大写
- 四则运算程序扩展:将程序改为java语言,并允许用户输入,对输入结果进行验证
- 3. 串的处理 在实际的开发工作中,对字符串的处理是最常见的编程任务。 本题目即是要求程序对用户输入的串进行处理。具体规则如下: 1. 把每个单词的首字母变为大写。 2. 把数字与
- 通过键盘输入一串小写字母(a~z)组成的字符串。请编写一个字符串过滤程序,若字符串中出现多个相同的字符,将非首次出现的字符过滤掉。 比如字符串“abacacde”过滤结果为“abcde”。 要求实现函