【发布时间】:2011-05-16 22:13:39
【问题描述】:
我知道“while (((str.nextToken()!=str.TT_EOL)))”是一个无限循环,但我想知道如何以与 StringTokenizer 中“while”类似的方式结束这个循环(st.hasMoreTokens())",它只需要读取 20 个令牌,这是我的代码: 非常感谢您的帮助:
import java.io.*;
import java.util.Arrays;
public class StonePile {
public static void main(String args[]) throws IOException {
StreamTokenizer str = new StreamTokenizer(new BufferedReader(new InputStreamReader(System.in)));
//int diff = 0;
str.nextToken();
int[] array = new int[(int) str.nval + 1];
int array2[] = new int[array.length - 1];
//System.out.println(array.length);
int i = 0;
int diff = 0;
while (((str.nextToken() != str.TT_EOL))) {
if (str.ttype == StreamTokenizer.TT_NUMBER) {
System.out.println(i);
array[i++] = (int) str.nval;
}
// if (i > 4) {
// break;
// }
}
for (int j = 0; j < array.length - 2; j++) {
diff = Math.abs(array[j + 1] - array[j]);
array2[j] = diff;
// System.out.println("El array es en" + j+" es"+ array2[j]);
}
Arrays.sort(array2);
System.out.println(array2[1]);
}
}
【问题讨论】:
-
你只需要读取20个token或者20个TT_NUMBER?
-
可以是 1 到 20 个令牌
标签: java iostream inputstream