如何在 Java 的 Scanner 实用程序中将 Input 放在下一行

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18754847/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 10:46:00  来源:igfitidea点击:

How to put Input on next line in Java's Scanner utility

javajava.util.scannerprintln

提问by VrnRap

I have a basic java question about the scanner utility. Let's say i have a simple program that takes the user input and stores it in a variable. My question is when i run the program that asks for multiple inputs, the cursor starts at the beginning of the question and not after it. My code is:

我有一个关于扫描仪实用程序的基本 Java 问题。假设我有一个简单的程序,它接受用户输入并将其存储在一个变量中。我的问题是当我运行要求多个输入的程序时,光标从问题的开头而不是之后开始。我的代码是:

public class question3 {

    public static void main(String[] args){

       Scanner s = new Scanner(System.in);
       System.out.println("Enter the first number:");
       Float a = s.nextFloat();
       System.out.println("Enter the second number:");
       Float b = s.nextFloat();
       System.out.println("Sum = " + (a+b));
       System.out.println("Difference = " + (a-b));
       System.out.println("Product = " + (a*b));


       }
}

When I run this program it will look like Enter First Number then i type the number, and then |Enter Second Number. "|" meaning where the blinking cursor is. When I type it'll show up underneath, but it could confuse the user so I was wondering what the solution could be.

当我运行这个程序时,它看起来像输入第一个数字,然后我输入数字,然后|输入第二个数字。“|” 意思是闪烁的光标所在的位置。当我输入时,它会显示在下面,但它可能会使用户感到困惑,所以我想知道解决方案是什么。

enter image description here

在此处输入图片说明

It is an IDE problem, since nothing else is wrong with the code.

这是一个IDE问题,因为代码没有其他问题。

采纳答案by Josh M

Instead of println(String)before each input, change it to print(String). So it would look something like this:

而不是println(String)在每个输入之前,将其更改为print(String). 所以它看起来像这样:

public class question3{

    public static void main(String[] args){

        Scanner s = new Scanner(System.in);
        System.out.print("Enter the first number:");
        Float a = s.nextFloat();
        System.out.print("Enter the second number:");
        Float b = s.nextFloat();
        System.out.println("Sum = " + (a+b));
        System.out.println("Difference = " + (a-b));
        System.out.println("Product = " + (a*b));


    }
}

Also, just a note, you should use proper/appropriate naming conventions for your variables. Like for your Scanner, you should call it readeror input; something which represents its function. The same idea goes for the rest of your variables. Also, class names start with a capital.

另外,请注意,您应该为变量使用正确/适当的命名约定。就像你的一样Scanner,你应该称它为readerinput;代表其功能的东西。同样的想法也适用于您的其余变量。此外,类名以大写开头。

Here is what the finished result looks like:

最终结果如下所示:

enter image description here

在此处输入图片说明

回答by ug_

System.out.println prints out string then a new line, so your input is being placed on a new line. Try making it read

System.out.println 打印出字符串,然后是一个新行,因此您的输入被放置在一个新行上。尝试让它阅读

System.out.print("Enter the first number:");
Float a = s.nextFloat();
System.out.println();
System.out.print("Enter the second number:");
Float b = s.nextFloat();
System.out.println();

回答by LeandreM

This can save you some seconds, by typing few lines:

通过输入几行,这可以为您节省几秒钟:

System.out.print("Enter the first number:");
Float a = s.nextFloat();
System.out.print("\nEnter the second number:");
Float b = s.nextFloat();
System.out.println("\nSum = " + (a+b)
                   +"\nDifference = " + (a-b)
                   +"\nProduct = " + (a*b));