[英]Printing strings using PrintWriter
Problem Description: 问题描述:
I am expecting to print the
Press
options first then take the user input but usingout.println
i can't do that.我期望先打印
Press
选项,然后接受用户输入,但是使用out.println
我无法做到这一点。 It's not printing the options while i run the program.我运行程序时没有打印选项。 It just waits for an input while i give the input it then prints the Press options.
它只是等待输入,而我给它输入,然后打印Press选项。
Note: if i use System.out.println
then it works as expected. 注意: 如果我使用
System.out.println
那么它将按预期工作。
Tried it like below 尝试如下
public class Calculator {
public static void main(String[] args) throws IOException {
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
PrintWriter out = new PrintWriter(new OutputStreamWriter(System.out));
StringTokenizer took = new StringTokenizer(" ");
String operator;
// System.out.println("Press '+' for Additon");
// System.out.println("Press '-' for Subtraction");
// System.out.println("Press '*' for multiplication");
out.println("Press '+' for Additon");
out.println("Press '-' for Subtraction");
out.println("Press '*' for multiplication");
out.println("Press '/' for Division");
operator=in.readLine();
switch (operator) {
case "+":
out.println("Add");
break;
case "-":
out.println("Subtract");
break;
case "*":
out.println("Multiply");;
break;
case "/":
out.println("Division");
break;
}
in.close();
out.close();
}
}
Input: 输入:
*
Output: 输出:
Press '+' for Additon
Press '-' for Subtraction
Press '*' for multiplication
Press '/' for Division
Multiply
Do: 做:
out.flush();
Before you read in, and then before you close the out
. 在您阅读,然后关闭前
out
。
Or you can do: 或者,您可以执行以下操作:
PrintWriter out = new PrintWriter(new OutputStreamWriter(System.out), true);
What the true
does is sets it to auto flush every time you print. true
作用是将其设置为在每次打印时自动刷新。
Suggest reviewing the Javadoc for the PrintWriter
. 建议复查
PrintWriter
的Javadoc。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.