问一个JAVA问题

l6887727 2008-10-07 12:59:11
有个题目做不来

从键盘输入一系列字符,以#号作为结束标记,求这些字符中的最小者。
注意:输入数据建议采用一行输入(例如:abdhg34dg#)

哪位大哥教下俺咋做出来,不胜感激!
...全文
177 12 打赏 收藏 转发到动态 举报
写回复
用AI写文章
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
zhouxingyu896 2008-10-10
  • 打赏
  • 举报
回复
使用ASCII 进行比较后,然后在排序。
wNvShine 2008-10-09
  • 打赏
  • 举报
回复
[Quote=引用 8 楼 ThirstyCrow 的回复:]
Java codepublic class Main {

public static void main(String[] args) throws IOException {
Reader reader = new BufferedReader(new InputStreamReader(System.in));
int c = reader.read();
int min = c;
while ((c = reader.read()) != '#') {
if (c < min) {
min = c;
}
}
System.out.println((char) min);

[/Quote]


这不就是了么..这种问题还是要多自己做呀..LZ..不然以后什么问题都不会想了
wNvShine 2008-10-09
  • 打赏
  • 举报
回复
用ASCII码比较不就完了么...把接收到的字符都转换成对应的ASCII码
然后再一一比较
保留最小的..再转换成char输出
liky5387 2008-10-09
  • 打赏
  • 举报
回复
应该在用以下方式吧???因为System.in只能接收到enter的时候他才能认为此次输入结束了.没法能过一个"#"来判断.所以只能用如下方式.不知道对不对.

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Arrays;

public class Test3 {
public static void main(String[] args) throws IOException {
StringBuffer strb = new StringBuffer();
String str = "";
while (true) {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
str = br.readLine();
if (str.endsWith("#")) {
break;
}
strb.append(str);
}
char[] cha = strb.toString().toCharArray();
Arrays.sort(cha);
for (int i = 0; i < cha.length; i++) {
System.out.println(cha[i]);
}
}
}
ThirstyCrow 2008-10-08
  • 打赏
  • 举报
回复
public class Main {

public static void main(String[] args) throws IOException {
Reader reader = new BufferedReader(new InputStreamReader(System.in));
int c = reader.read();
int min = c;
while ((c = reader.read()) != '#') {
if (c < min) {
min = c;
}
}
System.out.println((char) min);
}
}
xuhua205 2008-10-08
  • 打赏
  • 举报
回复
我是这样觉得:
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String str = null;
while ((str = br.readLine()) != null) {
if (str.endWith("#")) {
return;
}
}
char[] ch = str.toCharArray();
Arrays.sort(ch);
System.out.println(ch[0]);
lordtan 2008-10-07
  • 打赏
  • 举报
回复
支持4楼
yuchui 2008-10-07
  • 打赏
  • 举报
回复
楼上的做法不错就是这样
myminer 2008-10-07
  • 打赏
  • 举报
回复
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringBuffer sb = new StringBuffer();
String line;
while(null != (line = br.readLine()))
{
sb.append(line);
if(true == line.endsWith("#"))
{
break;
}
}
int flag = sb.charAt(0);
for(int i = 1; i < sb.length() - 1; i ++)
{
if(flag > sb.charAt(i))
{
flag = sb.charAt(i);
}
}
System.out.print((char)flag);
uYuOmooo 2008-10-07
  • 打赏
  • 举报
回复
一楼想法比较好,可以去试一下
ouyangxiaokang6 2008-10-07
  • 打赏
  • 举报
回复
把键盘中这些字符都 输进一个 String里面.求出ascii码然后去最小的。
cydp007 2008-10-07
  • 打赏
  • 举报
回复
把键盘中这些字符都 输进一个 String里面.

然后转成一个数组.. 然后sort一下..

把第一个拿出来就好了.

62,614

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧