Java 如何在eclipse中获取用户输入?

Java 如何在eclipse中获取用户输入?,java,Java,嗨,如何使用eclipse获取用户输入。就像在命令提示符中一样 C:/javajavaapp(此处为参数)。如何使eclipse从用户获取输入。?运行-->运行配置-->参数(它是右侧的第二个选项卡)-->程序参数在程序中添加此行以从控制台接受用户输入: BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); 然后在希望从运行时接受输入的任何变量旁边添加in.readLine()。比方说,您想将coun

嗨,如何使用eclipse获取用户输入。就像在命令提示符中一样


C:/javajavaapp(此处为参数)。如何使eclipse从用户获取输入。?

运行-->运行配置-->参数(它是右侧的第二个选项卡)-->程序参数在程序中添加此行以从控制台接受用户输入:

BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
然后在希望从运行时接受输入的任何变量旁边添加in.readLine()。比方说,您想将count变量初始化为值1,那么,它应该写为

int count=in.readLine()

运行程序后,应在控制台中输入值1。以下是在Eclips中获取用户输入的一种方法

import java.util.Scanner; // enter the before the start of any class 
// declare the class here 
// this is an example of how to use the Scanner in a method
Public static void Username();{
Scanner Input = new scanner (System.in);
String username// declaring the username variable 
Scanner un = new Scanner(System.in); //  un is just a random variable you can choose any other variable name if you want
System.out.println("Enter Your Username");
username = un.next(); 
System.out.println("The username you entered is : " + username);}
但是如果你想在这里输入一个整数或双精度,你该怎么做呢。 我将给出一个int输入的例子,用double替换int

import java.util.Scanner; 
// Declare the class here
// this is an example of how to use the Scanner in a method for an integer input by user
Public static void booksRead();{
Scanner Input = new scanner (System.in);
int books // declaring the books variable 
Scanner br = new Scanner(System.in); //  br is just a random variable you can choose any other variable name if you want
System.out.println("Enter how many books have you read: ");
books = br.next(); 
System.out.println("The number of books you have read is : " + books);}

这将打印用户输入的号码:

public static void main(String[] args) {
    // TODO Auto-generated method stub
    Scanner reader = new Scanner(System.in);  // Reading from System.in
    System.out.println("Enter a number: ");
    int n = reader.nextInt(); // Scans the next token of the input as an int.
    //once finished
    System.out.println(n);
    reader.close(); 
}

上面的解决方案只是在运行时接受用户输入的另一种方式,而不是上述问题的答案。请注意+单击
变量
并选择
string\u提示符