import java.io.BufferedReader
import java.io.InputStreamReader
import java.util.Arrays
public class NumberSplit {
public StringBuffer getString(String str) {
StringBuffer strbuf = new StringBuffer("t")
boolean flag = true
for (int i = 0i <str.length()i++) {
char c = str.charAt(i)
// 判断是否是数字
if (c >= '0'&&c <= '9') {
// 判断和字符串中的数字是否重液慧复
for (int j = 1j <strbuf.length()j++) {
if (c == strbuf.charAt(j)) {
// 如果重复,标志位数值为false,并跳出循环否则标志位设置为true.
flag = false
break
} else
flag = true
}
// 只有在即是数字又不重复的情况下才将改字符拼接到字符串上.
if (flag) {
strbuf.append(c)
}
}
}
return strbuf
}
public static void main(String args[]) throws Exception {
System.out.println("请输入一段字符串,并以回车结束")
BufferedReader buf = new BufferedReader(
new InputStreamReader(System.in))
String str = buf.readLine()
StringBuffer strbuf = new NumberSplit().getString(str)
// 将返回的StringBuffer转换为字符数组
char c[] = strbuf.deleteCharAt(0).toString().toCharArray()
// 对字符数组排序
Arrays.sort(c)
// 输出字符数组
for (int i = 0i <strbuf.length()i++) {
System.out.print(c[i])
}
}
import java.io.BufferedReader
import java.io.InputStreamReader
import java.util.Arrays
public class NumberSplit {
public StringBuffer getString(String str) {
StringBuffer strbuf = new StringBuffer("t")
boolean flag = true
for (int i = 0i <str.length()i++) {
char c = str.charAt(i)
// 判断是否是空埋缓数字
if (c >= '0'&&c <= '9') {
// 判断和字符串中的数字是否重复
for (int j = 1j <strbuf.length()j++) {
if (c == strbuf.charAt(j)) {
// 如果重复,标志位数值为false,并跳出循环否则标志位设置为true.
flag = false
break
} else
斗模 flag = true
}
// 只有在即是数字又不重复的情况下才将改字符拼接到字符串上.
if (flag) {
strbuf.append(c)
}
}
}
return strbuf
}
public static void main(String args[]) throws Exception {
System.out.println("请输入一段字符串,并以回车结束")
BufferedReader buf = new BufferedReader(
new InputStreamReader(System.in))
String str = buf.readLine()
StringBuffer strbuf = new NumberSplit().getString(str)
// 将返回的StringBuffer转换为字符数组
char c[] = strbuf.deleteCharAt(0).toString().toCharArray()
// 对字符数组排序
Arrays.sort(c)
// 输出字符数组
for (int i = 0i <strbuf.length()i++) {
System.out.print(c[i])
}
}
分类: 电脑/网络 >>程序设计 >>其他编程颤埋亏语言问题描述:
怎么用java把输液肆入的字符串中的数字提取出来,比如输入的是a0s9ad1sj5a0h1fus6a,输出的结果是0915016。谢谢了……
最好用简单点的语法,我的java初学者,高手帮帮啊……
解析:
import java.io.BufferedReader
import java.io.InputStreamReader
import java.util.Arrays
public class NumberSplit {
public StringBuffer getString(String str) {
StringBuffer strbuf = new StringBuffer("t")
boolean flag = true
for (int i = 0i <str.length()i++) {
char c = str.charAt(i)
判断是否是数字
if (c >= '0' &&c <= '9') {
判断和字符串中的数字是否重复
for (int j = 1j <strbuf.length()j++) {
if (c == strbuf.charAt(j)) {
如果重复,标志位数值为false,并跳出循环否则标茄神志位设置为true.
flag = false
break
} else
flag = true
}
只有在即是数字又不重复的情况下才将改字符拼接到字符串上.
if (flag) {
strbuf.append(c)
}
}
}
return strbuf
}
public static void main(String args[]) throws Exception {
System.out.println("请输入一段字符串,并以回车结束")
BufferedReader buf = new BufferedReader(
new InputStreamReader(System.in))
String str = buf.readLine()
StringBuffer strbuf = new NumberSplit().getString(str)
将返回的StringBuffer转换为字符数组
char c[] = strbuf.deleteCharAt(0).toString().toCharArray()
对字符数组排序
Arrays.sort(c)
输出字符数组
for (int i = 0i <strbuf.length()i++) {
System.out.print(c[i])
}
}
}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)