来自具有gets()的用户的输入

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了来自具有gets()的用户的输入相关的知识,希望对你有一定的参考价值。

我编写了一个程序来获取用户的输入,但现在我应该使用gets()来获取输入。

我应该改变整个代码吗?我无法找到如何在代码中使用gets()..这是我的程序没有gets():

package com.company;

import java.util.Scanner;

class HelloYou
{

    public static void main(String[] args) {

        Scanner user_input = new Scanner(System.in) ;

        String first_name;
        System.out.print("Enter your first name: ");
        first_name=user_input.nextLine();


        System.out.println("Hello," + " " + first_name + '!');
    }


}
答案

一种方法:

package View;

import java.util.Scanner;


public class View {

private Scanner in;
private String name;

 public View() {

    this.in = new Scanner(System.in);


 }

 public void askUserName(){
    this.name = this.in.next();
 }


 public String getName(){
    return this.name;
 }
}

但是不要忘记检查用户输入的值之前......

以上是关于来自具有gets()的用户的输入的主要内容,如果未能解决你的问题,请参考以下文章