关于JAVA的一个小问题.
登录 | 论坛导航 -> 华新鲜事 -> 求学狮城 | 本帖共有 3 楼,分 1 页, 当前显示第 1 页 : 本帖树形列表 : 刷新 : 返回上一页
<<始页  [1]  末页>>
作者:crycrycry (等级:2 - 初出茅庐,发帖:27) 发表:2003-05-30 10:28:31  楼主  关注此帖
关于JAVA的一个小问题.
我想一次输入很多的数直,
输出格式是: Enter 3 lengths:2,3,3
Triangle is:true

我只知道每次读去一个数据,现在有三个,该怎么办呢?

谢谢先!!!
Put your OWN COOL signature here!
欢迎来到华新中文网,踊跃发帖是支持我们的最好方法!原文 / 传统版 / WAP版只看此人从这里展开收起列表
作者:Flying (等级:18 - 华新水车,发帖:16849) 发表:2003-05-30 10:42:08  2楼 评分:
Usual approaches:
1) Read a line (enter-terminated), the use string tokenizer to tokenize (slice) the string into substrings before parsing them for numbers. Sample code fragment:

BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
String s = in.readLine();
StringTokenizer st = new StringTokenizer(s, ", \t\f\n");
int[] num = new int[3];
for (int i = 0; i < 3; i++) {
String n = st.nextToken();
num[i] = Integer.parseInt(n);
}
//I didn't check for valid input/correct number of input here. You've got to patch those possible errors.

2) This approach is more difficult to learn, but much more elegant. Use java.text.MessageFormat class to perform a formated input parsing. It is similar to the way C handles input (format string pattern matching), but its syntax is different, and is in some sense more flexible.
欢迎来到华新中文网,踊跃发帖是支持我们的最好方法!原文 / 传统版 / WAP版只看此人从这里展开收起列表
作者:crycrycry (等级:2 - 初出茅庐,发帖:27) 发表:2003-05-30 10:47:59  3楼
Usual approaches:1) Read a line (enter-terminated), the use string tokenizer to tokenize (slice) the string into substrings before parsing them for numbers. Sample code fragment: BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); String s = in.readLine(); StringTokenizer st = new StringTokenizer(s, ", \t\f\n"); int[] num = new int[3]; for (int i = 0; i < 3; i++) { String n = st.nextToken(); num[i] = Integer.parseInt(n); } //I didn't check for valid input/correct number of input here. You've got to patch those possible errors. 2) This approach is more difficult to learn, but much more elegant. Use java.text.MessageFormat class to perform a formated input parsing. It is similar to the way C handles input (format string pattern matching), but its syntax is different, and is in some sense more flexible.
thanks a lot. i will try! :)
欢迎来到华新中文网,踊跃发帖是支持我们的最好方法!原文 / 传统版 / WAP版只看此人从这里展开收起列表
论坛导航 -> 华新鲜事 -> 求学狮城 | 返回上一页 | 本主题共有 3 篇文章,分 1 页, 当前显示第 1 页 | 回到顶部
<<始页  [1]  末页>>

请登录后回复:帐号   密码