一.获得控制台用户输入的信息
public
String getInputMessage()
throws
IOException
{
System.out.println(
"
请输入您的命令∶
"
);
byte
buffer[]
=
new
byte
[
1024
];
int
count
=
System.in.read(buffer);
char
[] ch
=
new
char
[count
-
2
];
//
最后两位为结束符,删去不要
for
(
int
i
=
0
;i
<
count
-
2
;i
++
)
ch[i]
=
(
char
)buffer[i];
String str
=
new
String(ch);
return
str;
}
二.字符串数组排序
/**
对字符串数组进行排序
*
@param
str 原始字符串数组
*
@param
flag flag=0:顺序排序 flag=1:倒序排序
*
@return
排序后的字符串数组
*/
public
String[] sort(String[] str,
int
flag)
{
if
(str
==
null
||
str.length
==
0
)
throw
new
IllegalArgumentException();
String temp
=
str[
0
];
//
顺序排列 ,即从小到大
if
(flag
==
0
)
{
for
(
int
i
=
0
;i
<
str.length
-
1
;i
++
)
{
for
(
int
j
=
i
+
1
;j
<
str.length;j
++
)
{
if
(str[i].compareTo(str[j])
>
0
)
{
temp
=
str[i];
str[i]
=
str[j];
str[j]
=
temp;
}
}
}
}
else
if
(flag
==
1
)
{
//
倒序排列
for
(
int
i
=
0
;i
<
str.length
-
1
;i
++
)
{
for
(
int
j
=
i
+
1
;j
<
str.length;j
++
)
{
if
(str[i].compareTo(str[j])
<
0
)
{
temp
=
str[i];
str[i]
=
str[j];
str[j]
=
temp;
}
}
}
}
return
str;
}