黑马程序员技术交流社区
标题:
大家看看我的代码,输出的老是ascii码
[打印本页]
作者:
xiaoxinxin003
时间:
2015-7-15 00:10
标题:
大家看看我的代码,输出的老是ascii码
package com.itheima;
import java.util.*;
/*
* 定义一个静态方法,该方法可以接收一个List<Integer>,方法内对List进行排序
* */
public class test9 {
public static void main(String[] args) throws Exception {
Method();
}
public static void Method() throws Exception
{
//BufferedReader bufr = new BufferedReader(new InputStreamReader(System.in));
Scanner sc = new Scanner(System.in);
System.out.println("请输入需要排序的数字: ");
//用字符串的形式接收。
String str = new String();
//str = bufr.readLine();
str = sc.nextLine();
sc.close();
//用正则表达式判断获取的是不是全为数字。
if(!(str.matches("[0-9]+")))
{
System.out.println("抱歉,只接受数字!");
return;
}
System.out.println("你输入的原始数据为: "+str);
//创建字节数组
char[] buf = str.toCharArray();
//字符串转换成字节数组并存放到字节数组
List<Integer> list = new ArrayList<Integer>();
for(int x=0;x<buf.length;x++)
{
//System.out.print(buf[x]);
Integer temp = (int)buf[x];
list.add(temp);
}
sort(list);
for(int i=0;i<list.size();i++)
{
System.out.print( list.get(i)+" ");
}
}
private static void sort(List<Integer> list)
{
Collections.sort(list);
}
}
复制代码
作者:
李文思
时间:
2015-7-15 15:49
你搞错了.35行,你创建的是字符数组,在44行,如果你用int直接进行强转一个字符,
获取到的是该字符在ASCII表中对应的数,
你要想将字符1变成整数1.就要用Integer的方法.parseInt
试试
欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/)
黑马程序员IT技术论坛 X3.2