非终止Java程序2

发布于 2024-12-14 12:19:49 字数 875 浏览 1 评论 0原文

我遇到了与之前在此链接中发布的问题相同的问题。 我不知道为什么,但是当我再次尝试时,但这次使用 Eclipse(我在链接中使用了 Netbeans)。我得到的结果是完全错误的。在 UVAToolkit 中,误导性的空白可能会返回错误的答案。

如果有机会的话,我应该使用 BufferedReader 代替吗?

BufferedReader reader = new BufferedReader(InputStreamReader(System.in));

代码 [注:位于 main(String[] args) 方法中]

Scanner scanner = new Scanner(System.in);
String line;
while(!(line = scanner.nextLine()).equals("")) {
     System.out.println(line);
}
System.out.println("done");

案例输入:[“1000”字符串末尾没有空格]

1 10
1 100
1 1000

案例输出 [注:“换行”属于空格输入]

1 10
1 100
"new-line"
1 1000
"new-line"
done
"new-line"

正确的大小写输出 [注意:“done”字符串末尾没有空格]

1 10
1 100
1 1000
done

提前致谢。

I have the same problem as before posted in this link.
I don't why but when I tried it again but this time using Eclipse (I used Netbeans in the link). I get this results which is strictly wrong. In UVAToolkit, ever misleading white-space could return a wrong answer.

If by any chance, should I use BufferedReader instead?

BufferedReader reader = new BufferedReader(InputStreamReader(System.in));

Code [Note: located in the main(String[] args) method]

Scanner scanner = new Scanner(System.in);
String line;
while(!(line = scanner.nextLine()).equals("")) {
     System.out.println(line);
}
System.out.println("done");

Case Input: [No white-space at the end of "1000" string]

1 10
1 100
1 1000

Case Output [Note: "new-line" pertains to a white-space enter ]

1 10
1 100
"new-line"
1 1000
"new-line"
done
"new-line"

Correct Case Output [Note: No white-space at the end of the "done" string]

1 10
1 100
1 1000
done

Thanks in advance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

财迷小姐 2024-12-21 12:19:49

以下代码将显示输入的结果字符串(如果它不是空字符串),并在完成时显示“done”。

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class ReadConsoleScanner {
    public static void main(String[] args) {
        boolean stop = false;
        BufferedReader bufferRead = new BufferedReader(new InputStreamReader(System.in));
        String sWhatever = "";
        try {
            while(!(stop = (sWhatever = bufferRead.readLine()).matches(""))){
                if (!stop) System.out.println(sWhatever);
            }
            System.out.println("done");
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

    }
}

关于您的期望如下:

案例输入:[“1000”字符串末尾没有空格]

我认为很难找到一种方法来避免空格(空字符串),这是循环的哨兵所需要的,除非最终输入是预期的为“1 1000”,我们可以按如下方式更改循环的哨兵:

while(!(stop = (sWhatever = bufferRead.readLine()).contains("1 1000"))){
    //whatever
}

Following code will display the result string of input if it is not an empty string and display "done" when it is done.

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class ReadConsoleScanner {
    public static void main(String[] args) {
        boolean stop = false;
        BufferedReader bufferRead = new BufferedReader(new InputStreamReader(System.in));
        String sWhatever = "";
        try {
            while(!(stop = (sWhatever = bufferRead.readLine()).matches(""))){
                if (!stop) System.out.println(sWhatever);
            }
            System.out.println("done");
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

    }
}

Regarding your expectation below:

Case Input: [No white-space at the end of "1000" string]

I think it is quite difficult to find a way to avoid whitespace (empty string) which is needed as the sentinel to the loop, unless the final input is expected to be "1 1000" where we can change the sentinel of the loop as follows:

while(!(stop = (sWhatever = bufferRead.readLine()).contains("1 1000"))){
    //whatever
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文