简体   繁体   中英

how to take user inputs in eclipse?

Hi how to take user inputs using eclipse. Like in command prompt we do

C:/ java javaApp (arguments here). How to make eclipse to take inputs from the user.?

运行 - >运行配置 - >参数(它是右边的第二个选项卡) - >程序参数

Add this line in your program to accept user's input from the console:

BufferedReader in = new BufferedReader(new InputStreamReader(System.in));

and then add in.readLine() beside whatever variable you want to accept input from Runtime. Lets say, you want to initialize count variable to the value 1, then, it should be written as

int count = in.readLine(); The value 1 should be inputted in the console after you run the program

Here is one way to take user input in 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);}

But if you want to take a integer or double as a input here how you do it. I am just going to give an example for int input you just have replace the int with double.

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);}

This will print the number entered by user :

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(); 
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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