怎么用system.in来读取多个数值

uqortbsa 2010-07-05 05:16:53
怎么用system.in来读取多个数值
...全文
275 16 打赏 收藏 转发到动态 举报
写回复
用AI写文章
16 条回复
切换为时间正序
请发表友善的回复…
发表回复
uqortbsa 2010-08-16
  • 打赏
  • 举报
回复
java的输入是有点繁琐的,system.in只是获取一个二进制流
uqortbsa 2010-08-16
  • 打赏
  • 举报
回复
System.in.read()函数接受键盘输入值的ASKII码
uqortbsa 2010-08-16
  • 打赏
  • 举报
回复
import java.util.Scanner;

/**

  * 测试从键盘读取用户的输入,关键类为Scanner

  * @author Administrator

  *

  */

public class TestInput{
public static void main(String args[]){
Scanner s = new Scanner(System.in);

System.out.println("请输入你的姓名:");
String name = s.nextLine();

System.out.println("请输入你的年龄:");

int age = s.nextInt();

System.out.println("请输入你的工资:");

float salary = s.nextFloat();

System.out.println("你的信息如下:");

System.out.println("姓名:"+name+"\n"+"年龄:"+age+"\n"+"工资:"+salary);


}
}


lianghua19 2010-07-14
  • 打赏
  • 举报
回复
Scanner scanner =new Scanner(System.in);
String str=scanner.next();
for(int i=0;i<str.length();i++){
str.charAt(i);
}
oudoud 2010-07-09
  • 打赏
  • 举报
回复
循环!
thegodofwar 2010-07-09
  • 打赏
  • 举报
回复
可以用那个
public static void main(String []args) {
...
}

也可用args数组来读取
coooliang 2010-07-08
  • 打赏
  • 举报
回复

import java.util.Scanner;
public class Test
{
public static void main(String[] args)
{
Scanner in = new Scanner(System.in);
String s = in.nextLine();
String[] num = s.split(" ");
int[] lastNum = new int[num.length];
for(int i=0;i<num.length;i++){
int x = Integer.parseInt(num[i]);
lastNum[i] = x;
}
for(int l :lastNum){
System.out.println("Your enter num is:" + l);
}

}
}

在shell中输入的数之间要用 空格 分开
forgood 2010-07-08
  • 打赏
  • 举报
回复
学习了,呵呵
/*public static void main(String[] args){
while(true)
{
Scanner s =new Scanner(System.in);
System.out.println(s.nextLine());
if(s.nextLine().equals("exit"))
{
break;
}
}
}*/
public static void main(String[] args) {
InputStreamReader isr = new InputStreamReader(System.in);
BufferedReader br = new BufferedReader(isr);

String s = null;
try {
s = br.readLine();
while (s != null) {
if (s.equalsIgnoreCase("end"))
break;
System.out.println(s);
s = br.readLine();
}
br.close();

} catch (IOException e) {
e.printStackTrace();
}
}
chuchuzinnia 2010-07-07
  • 打赏
  • 举报
回复

public class TestTransform2 {
public static void main(String[] args) {
InputStreamReader isr = new InputStreamReader(System.in);
BufferedReader br = new BufferedReader(isr);

String s = null;
try {
s = br.readLine();
while (s != null) {
if (s.equalsIgnoreCase("end"))
break;
System.out.println(s);
s = br.readLine();
}
br.close();

} catch (IOException e) {
e.printStackTrace();
}
}
}
liu_siat 2010-07-05
  • 打赏
  • 举报
回复
读取一行字符串,解析成多个数据
或循环读取
jn789987 2010-07-05
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 minzhong20 的回复:]
对于读取多个值你可以用while循环来读取

while(true)
{
Scanner s =new Scanner(System.in);
System.out.println(s.nextLine());
if(s.nextLine().equals("exit"))
{
break;
}
}
[/Quote]
五楼 盗我的版~~
minzhong20 2010-07-05
  • 打赏
  • 举报
回复
对于读取多个值你可以用while循环来读取

while(true)
{
Scanner s =new Scanner(System.in);
System.out.println(s.nextLine());
if(s.nextLine().equals("exit"))
{
break;
}
}
jn789987 2010-07-05
  • 打赏
  • 举报
回复
s.nextLine() 读取的是一行数据,如果你的数据用分割符分开可以用split将其分割开来:

String str[] = s.nextLine().split("");
closewbq 2010-07-05
  • 打赏
  • 举报
回复
循环读取吧!
jn789987 2010-07-05
  • 打赏
  • 举报
回复
对于读取多个值你可以用while循环来读取

while(true)
{
Scanner s =new Scanner(System.in);
System.out.println(s.nextLine());
if(s.nextLine().equals("exit"))
{
break;
}
}
dr_lou 2010-07-05
  • 打赏
  • 举报
回复
一次只能一个,多个是什么意思。
用1,2,3,4 自己分隔吧。

62,614

社区成员

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

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