为什么我的程序永远无法达到解决方法?
抱歉,如果这是一个愚蠢的问题,但我是一个使用 StreamTokenizer 的初学者,我正在尝试解决这个练习 这个,请帮助我,我不知道我的程序有什么问题,永远不会达到我的解决方法,它也永远不会完成,我已经在 timus 论坛上问过,但我知道这里更快收到答复
import java.io.*;
public class Prueba {
static int index = 0;
static double[] l = new double[131072];
public static void main(String args[]) throws IOException {
StreamTokenizer str = new StreamTokenizer(new BufferedReader(new InputStreamReader(System.in)));
while (((str.nextToken() != StreamTokenizer.TT_EOF))) {
if (str.ttype == StreamTokenizer.TT_NUMBER) {
l[index++] = str.nval;
//System.out.println(str.nval);
// System.out.println(l[0]);
// System.out.println(l[1]);
}
}
solve();
}
public static void solve() {
double res;
for (int i = index - 1; i >= 0; i--) {
res = Math.sqrt(l[i]);
System.out.println(String.format("%.4f\n", res));
}
}
}
sorry if its a stupid question, but I a beginner using StreamTokenizer, I am trying to solve this exercise this, please help me, I dont know what its wrong in my program that never reach my solve method, it also never finishes, I already ask in timus forum, but I know that here is faster to receive an answers
import java.io.*;
public class Prueba {
static int index = 0;
static double[] l = new double[131072];
public static void main(String args[]) throws IOException {
StreamTokenizer str = new StreamTokenizer(new BufferedReader(new InputStreamReader(System.in)));
while (((str.nextToken() != StreamTokenizer.TT_EOF))) {
if (str.ttype == StreamTokenizer.TT_NUMBER) {
l[index++] = str.nval;
//System.out.println(str.nval);
// System.out.println(l[0]);
// System.out.println(l[1]);
}
}
solve();
}
public static void solve() {
double res;
for (int i = index - 1; i >= 0; i--) {
res = Math.sqrt(l[i]);
System.out.println(String.format("%.4f\n", res));
}
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您正在从标准输入中读取数据,并且您的代码会循环直到获得
TT_EOF
。要将TT_EOF
输入到您的程序中,如果您使用的是Unix,则需要按Ctrl-D
,或者按Ctrl-Z
后跟<如果您使用的是 Windows,请输入 code>Enter。You are reading from the standard input, and your code loops until it gets a
TT_EOF
. To feed aTT_EOF
into your program, you need to pressCtrl-D
if you're using Unix, orCtrl-Z
followed byEnter
if you're using Windows.您正在等待 System.in,它在读取时阻塞,因此,您永远不会到达 EOF,因此 while 循环将继续等待输入。
You are waiting on System.in, it is blocking on read, ergo, you will never get to EOF so you while loop will continue to wait for input.
事实上,您要么需要从命令行通过管道传输文件,要么在控制台上输入文本,后跟 EOF 字符。在 Windows 中按 Ctrl+Z 生成 EOF,在 Unix/Linux 中按 Ctrl+D 生成 EOF。
编辑:如果您的输入是单行,您可以检查TT_EOL 而不是 TT_EOF。
您必须调用 eolIsSignificant( true) 在进入循环之前。这将确保行尾被视为单独的标记
As it is, you either need to pipe a file from command line, or enter text on console followed by EOF character. Pressing Ctrl+Z generates EOF in Windows, and pressing Ctrl+D generates EOF in Unix/Linux.
EDIT: If your input is single line you can check for TT_EOL instead of TT_EOF.
You must call eolIsSignificant(true) before entering the loop. This will make sure end-of-line is treated as separate token