Java getString 方法

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/33044781/
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-11-02 21:06:57  来源:igfitidea点击:

Java getString method

javagetstring

提问by Jamie

So I have the questions in the top part, but I want to have all the questions at the top, and when I need to ask the questions, I can just pull them down using a variable defined as the question. Right now however, the code is asking the questions from where the questions are, not using the variable "ask" and asking from System.out.print(ask). Any ideas on how to get it to do that?

所以我在顶部有问题,但我希望所有问题都在顶部,当我需要提出问题时,我可以使用定义为问题的变量将它们拉下来。然而,现在代码是从问题所在的地方提出问题,而不是使用变量“ask”并从 System.out.print(ask) 中询问。关于如何让它做到这一点的任何想法?

import java.util.Scanner;

public class Greetings {

    public static void main(String[] args) {

        Scanner newscanner = new Scanner(System.in);

        String ask = getString(newscanner, "Please enter your first name: ");

        // String ask2 = getString(newscanner, "Please enter your last name: ");

        // String ask3 = getString(newscanner, "Please enter your year of birth:
        // ");

    }

    public static String getString(Scanner newscanner, String ask) {

        System.out.print(ask);

        String first = newscanner.next();

        String firstletter = first.substring(0, 1).toUpperCase();

        return firstletter;

    }

}

回答by AlphaModder

Perhaps what you are looking to do is have the question be printed, and then the answer typed on the line below it? If so, what you need to do is change the first call in getStringfrom System.out.printto System.out.println, which should add on a newline after the question, moving the input to the next line.

也许您想要做的是打印问题,然后在其下方的行中输入答案?如果是这样,您需要做的是将第一个调用getStringSystem.out.printto更改为System.out.println,它应该在问题之后添加一个换行符,将输入移到下一行。

EDIT: This is what it might look like now:

编辑:这就是它现在的样子:

Please enter your first name:John

And here's what it would change to:

这是它将更改为:

Please enter your first name:
John