2011-01-28 116 views
4

我有一个简单的客户端服务器程序,其中我有我的菜单中选择一个问题:是否有与Java相当的getchar?

do { 
    System.out.println("---- MENU ----\n"); 
    System.out.println("(drv) - List drives"); 
    System.out.println("(pwd) - Current path"); 
    System.out.println("(ls) - List files in a directory"); 
    System.out.println("(quit) - Quit program\n"); 
    System.out.print("Enter a command: "); 
    command = scanner.nextLine(); 
    sendMessage(command); 
    message = (String)in.readObject(); 
    System.out.println(message); 
    System.in.read(); 
}while(!command.equals("quit"));` 

我想打印屏幕上的结果,等到我按输入,然后再次显示菜单,所以我使用System.in.read()。我的问题是,它的第一次,但如果我选择其他选项,它不打印任何东西:

---- ---- MENU

(DRV) - 驱动器列表
(PWD) - 当前路径
(LS) - 在一个目录
(退出)列出文件 - 退出程序

输入命令:PWD
d:\ Documents和Settings \ asname \ workspace_java \服务器

---- ---- MENU

(DRV) - 驱动器列表
(PWD) - 当前路径
(LS) - 在一个目录
(退出)列出文件 - 退出程序

输入命令:ls

有没有在Java中的方法类似getchar,我可以改用?

回答

4

只需添加scanner.nextLine();System.out.println(message);经过这么等待

+0

不工作对我来说:( – Kobe 2011-01-28 19:10:08

相关问题