帮我看看这段程序!

hwansung 2002-12-11 08:19:08
import java.io.*;
class myinput
{
public static void main(String args[])
{
try
{
System.out.println("Enter Directory:");
char ch;
StringBuffer dirBuf = new StringBuffer();
while((ch=(char)System.in.read())!='\n')
dirBuf.append(ch);
System.out.println("Enter Filename:");
StringBuffer fileBuf = new StringBuffer();
while((ch=(char)System.in.read())!='\n')
fileBuf.append(ch);
File input = new File(dirBuf.toString(),fileBuf.toString());

if (input.isFile())
{
System.out.println("File Found!");
}
else
{
System.out.println("File not Found!");
}
}
catch(Exception e)
{
System.out.println("Error!");
}
}
}
如果能执行,请将结果贴出来,如果不能请帮我改一下
...全文
23 4 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
crm2000 2002-12-11
  • 打赏
  • 举报
回复
回车的时候伴随着'\r\n'两个字符的输入,所以后面加System.in.read()将后面那个\n过滤掉。
hwansung 2002-12-11
  • 打赏
  • 举报
回复
第一个改动我已经知道原因了,我以为换行符就是回车符,呵呵
hwansung 2002-12-11
  • 打赏
  • 举报
回复
能告诉我为什么改这两个地方吗?
1.'\n'改为'\r'
2.加了一句'System.in.read()'
knight_qmh 2002-12-11
  • 打赏
  • 举报
回复
import java.io.*;
class myinput
{
public static void main(String args[])
{
try
{
System.out.println("Enter Directory:");
char ch;
StringBuffer dirBuf = new StringBuffer();
while((ch = (char)System.in.read())!='\r')
dirBuf.append(ch);
System.out.println("Enter Filename:");
System.in.read();
StringBuffer fileBuf = new StringBuffer();
while((ch=(char)System.in.read())!='\r')
fileBuf.append(ch);
File input = new File(dirBuf.toString(),fileBuf.toString());

if (input.isFile())
{
System.out.println("File Found!");
}
else
{
System.out.println("File not Found!");
}
}
catch(Exception e)
{
System.out.println("Error!");
}
}
}

62,629

社区成员

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

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