在JAVA中怎样用system.in.read()读入字符串存到String中
答案:4 悬赏:40 手机版
解决时间 2021-03-31 01:08
- 提问者网友:缘字诀
- 2021-03-30 10:04
在JAVA中怎样用system.in.read()读入字符串存到String中
最佳答案
- 五星知识达人网友:独行浪子会拥风
- 2021-03-30 10:28
import java.io.*;
public class TestInput
{
public static void main(String args[])
{
InputStreamReader stdin = new InputStreamReader(System.in);//键盘输入
BufferedReader bufin = new BufferedReader(stdin);
try
{
String str = bufin.readLine();
}
catch(IOException E)
{
System.out.println("发生I/O错误!!!");
}
}
}
public class TestInput
{
public static void main(String args[])
{
InputStreamReader stdin = new InputStreamReader(System.in);//键盘输入
BufferedReader bufin = new BufferedReader(stdin);
try
{
String str = bufin.readLine();
}
catch(IOException E)
{
System.out.println("发生I/O错误!!!");
}
}
}
全部回答
- 1楼网友:逃夭
- 2021-03-30 12:09
public class Test{
public static void main(String []args)throws Exception{
byte[] b=new byte[1024];
System.out.print("请输入一个字符串:");
System.in.read(b);
String str=new String(b);
System.out.print(str);
}
}
你根据要输入的字符串的大小去设置byte数组的大小
public static void main(String []args)throws Exception{
byte[] b=new byte[1024];
System.out.print("请输入一个字符串:");
System.in.read(b);
String str=new String(b);
System.out.print(str);
}
}
你根据要输入的字符串的大小去设置byte数组的大小
- 2楼网友:动情书生
- 2021-03-30 10:59
题在哪里啊 ??
- 3楼网友:忘川信使
- 2021-03-30 10:41
该方法输入是console;返回值是int;保存的是console中输入的第一个字符的ASCII值;通过enter进行flush;
你可以用while循环将返回值保存在StringBuffer里面;在后再调用toString();
我个人很少用这个方法来读取console中的输入;个人感觉用InputStream会好一点;
你可以用while循环将返回值保存在StringBuffer里面;在后再调用toString();
我个人很少用这个方法来读取console中的输入;个人感觉用InputStream会好一点;
我要举报
如以上回答内容为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
点此我要举报以上问答信息
大家都在看
推荐资讯