当前位置: 代码迷 >> Java相关 >> [求助]请会的人教我..谢谢
  详细解决方案

[求助]请会的人教我..谢谢

热度:201   发布时间:2006-07-21 09:11:14.0
[求助]请会的人教我..谢谢

请问如何在java中实现输入语句,就像c语言里面的scanf语句


----------------解决方案--------------------------------------------------------
没人肯教我吗?
----------------解决方案--------------------------------------------------------
你可以好好看看书还有这个版的其他帖子
----------------解决方案--------------------------------------------------------
使用 System.in 来获取数据

例子
///////////这是获取一个字符的 例子
import java.io.*;
public class test{
public static void main(String[] args)throws IOException{
System.out.println("输入一个字符,ENTER来结束");
int i = System.in.read();
System.out.println("输入的是"+(char)r);
}

}
//////////你也可以使用BufferedReader 来获取一行数据
----------------解决方案--------------------------------------------------------
楼上的程序不行
System.out.println("输入的是"+(char)r);
r 应该改成i

----------------解决方案--------------------------------------------------------
用BufferReader也可以实现
但是要主要要抛出异常

基本实现是:
class check
{
public static void main(String[] args)throws Exception
{
BufferedReader input = new BufferedReader(new InputStreamReader(System.in));
String str = input.readLine();
}
}



----------------解决方案--------------------------------------------------------
谢了,小弟去试一下
----------------解决方案--------------------------------------------------------

以下代码只能在JDK5.0上跑

import java.util.*;

public class Test {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
System.out.println("你想输什么啊?");
String str = in.nextLine();
System.out.println("你想输这个啊:"+str);
}
}


----------------解决方案--------------------------------------------------------
  相关解决方案