求一个java程序代码_编写一个java程序,求代码

展开全部

这题貌似无聊,不过细想也涉及很多JAVA的编程基础知识。

这位triompha的这段程序好像也没有完全按照提问者的要e68a843231313335323631343130323136353331333332613130求作答。

他这段程序的运行结果是:

957a0ac4a92670426836d7b742a047ca.png

所以我这里也写了一个,先看运行结果:

968223985818e4881249a3bf6f7069b2.png

上代码:

import java.util.regex.Matcher;

import java.util.regex.Pattern;

public class Student{

public char[] name = new char[]{'无','聊','题'};

public int sid= 12123123;

public static void main(String[] args){

Student stu = new Student();

String strName = new String(stu.name);

System.out.println("1 name:" + strName + " sid:" + stu.sid);

System.out.println("2 Int2Hex:" + spaceStr(strName) + Integer.toHexString(stu.sid));

String uni = stringToUnicode(strName);

System.out.println("3 unicode:" + uni );

String cha = unicodeToString(uni);

System.out.println("4 change:" + cha + " unicode:" + stringToUnicode(cha) );

String sch = String.valueOf(stu.sid);

System.out.println("5 segment:" + cutSegment(cutSegment(sch,2,"-"),6,"-"));

}

//用于计算字符数并对其

public static String spaceStr(String s){

String str = "";

for (int i = 0; i 

str += " ";

}

return str;

}

//转换Unicode

public static String stringToUnicode(String s) {

String str = "";

for (int i = 0; i 

int ch = (int) s.charAt(i);

if (ch > 255)

str += "\\u" + Integer.toHexString(ch);

else

str += "\\" + Integer.toHexString(ch);

}

return str;

}

//转换字符,末位求反

public static String unicodeToString(String s) {

Pattern pattern = Pattern.compile("(\\\\u(\\p{XDigit}{4}))"); //正则表达式

Matcher matcher = pattern.matcher(s);

char ch;

while (matcher.find()) {

ch = (char) (Integer.parseInt(matcher.group(2), 16) ^ 1);//这里做了最后一位求反的特殊处理^1

s = s.replace(matcher.group(1), ch + "");

}

return s;

}

//插入分隔符

public static String cutSegment(String s, int i, String strInsert){

StringBuffer buff = new StringBuffer(s);

buff.insert(i,strInsert);

return buff.toString();

}

}

代码差不多要60行,这还真不是一个简单的问题啊。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值