Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/322.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 显示输出不符合预期_Java_Io - Fatal编程技术网

Java 显示输出不符合预期

Java 显示输出不符合预期,java,io,Java,Io,对于以下程序,我希望输出为 Enter any Value : 3 You Entered 3 3 Enter any value : You Entered 3. 但我并没有得到输出,相反,在输出中,java希望得到一些输入,而没有显示“输入任何值”。如果我输入任何值,它将显示输出为 Enter any Value : 3 You Entered 3 3 Enter any value : You Entered 3. 这是我的密码: import java.io.*;

对于以下程序,我希望输出为

Enter any Value : 3
You Entered 3
3
Enter any value : You Entered 3.
但我并没有得到输出,相反,在输出中,java希望得到一些输入,而没有显示“输入任何值”。如果我输入任何值,它将显示输出为

Enter any Value : 3
You Entered 3
3
Enter any value : You Entered 3.
这是我的密码:

    import java.io.*;
    // System.in is an object of InputStream - byte stream

    class ConsoleInputDemo{
        public static void main(String args[]) throws IOException{
            BufferedReader consoleInput = new BufferedReader(new InputStreamReader(System.in)); // This will convert byte stream to character stream
            PrintWriter consoleOutput = new PrintWriter(System.out); 

            consoleOutput.println("Enter any Value : ");
            int c= consoleInput.read();
            consoleOutput.write("You entered " + c);
            consoleInput.close();
            consoleOutput.close();

        }
    }

consoleOutput.println
直到它
close()
才打印任何内容。使用

 System.out.println("Enter any Value :  ")
而不是

consoleOutput.println("Enter any Value : ");
如果要查看文本,请关闭
ConsoleOutput

 consoleOutput.print("Enter any Value : ");
 consoleOutput.close();

这个简单的代码可能会帮助您实现您的需求。
read()
只会给您ascii值,所以我会使用
readline()
方法来获取实际值。当然,它将返回字符串,所以只需解析它就可以得到整数。

如果输入3,您可能得不到3,您将得到ASCII值。