Usual approaches:
所在版块:求学狮城 发贴时间:2003-05-30 10:42  评分:

用户信息
复制本帖HTML代码
高亮: 今天贴 X 昨天贴 X 前天贴 X 
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.
.
欢迎来到华新中文网,踊跃发帖是支持我们的最好方法!

Flying @way 吳穎暉
 相关帖子 我要回复↙ ↗回到正文
关于JAVA的一个小问题. crycrycry   (145 bytes , 270reads )
Usual approaches: Flying   (824 bytes , 152reads )
thanks a lot. i will try! :) crycrycry   (0 bytes , 154reads )