简体   繁体   English

在 java 中写入输入后出现消息

[英]Message appears after writing an input in java

I'm new to Java and i was learning inputs with Scanner but i'm getting something odd, the message of "enter a number" doesn't show, it only appears after I enter a value, here is the code and the screenshot.我是 Java 的新手,我正在使用 Scanner 学习输入,但我遇到了一些奇怪的事情,“输入数字”的消息没有显示,它只在我输入一个值后出现,这是代码和屏幕截图. I'm using java 11 btw.我正在使用 java 11 顺便说一句。 ` `

public class HolaMundoMain {

    public static void main(String[] args) {
        
        Scanner entrada = new Scanner(System.in);
        int numero;
        System.out.print("Enter a number: ");
        numero = entrada.nextInt();
        
        System.out.println("The number is: "+numero);
    }
}

` I tried writing the sout line before declaring Scanner but it does not work ` 我尝试在声明 Scanner 之前编写 sout 行,但它不起作用

I was expecting that first it would appear "Enter a number: " and then i could write it, but i have to write it first and then the message appears see image of terminal我期待首先它会出现“输入数字:”然后我可以写它,但我必须先写它然后出现消息看到终端的图像

The code it's working on my end.它在我这边工作的代码。 Try to compile it using javac HolaMundoMain.java and run with java -cp. HolaMundoMain尝试使用javac HolaMundoMain.java编译它并使用java -cp. HolaMundoMain java -cp. HolaMundoMain . java -cp. HolaMundoMain Image of the code running代码运行的图像

The code is perfectly fine, i tried running it as well and it does print first "Enter a number:" then after you enter value, it prints the next sout statement along with the value.代码非常好,我也尝试运行它,它确实首先打印“输入数字:”然后在您输入值后,它会打印下一个 sout 语句以及该值。 在此处输入图像描述 , the image shows my run of the said code. ,图像显示了我运行的上述代码。 Have you made any changes to java code?您是否对 Java 代码进行了任何更改? It's possible that you wrote the statement that takes input first then sout then you compiled it and ran it for the first time, but after changing it you forgot to compile it again using "javac completeFileName" in CMD.有可能您编写的语句首先接受输入然后 sout 然后编译并第一次运行它,但是在更改它之后您忘记了在 CMD 中使用“javac completeFileName”再次编译它。 I have a strong feeling you just need to recompile it and then run the program using "java completeFileName".我有一种强烈的感觉,您只需要重新编译它,然后使用“java completeFileName”运行该程序。 Also please note that, completeFileName is a name of path of file.另请注意,completeFileName 是文件路径的名称。 if you don't compile after making changes to a file, what happens is compiler runs the previously compiled version of it, which is basically a fileName.class file(you can even see it in folder), but when you do compile again a new fileName.class is created and then used to run.如果您在对文件进行更改后不编译,编译器会运行之前编译过的版本,基本上是一个 fileName.class 文件(您甚至可以在文件夹中看到它),但是当您再次编译时创建新的 fileName.class,然后用于运行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM