2011-02-07 22 views

回答

8

運行 - >運行配置 - >參數(這是右邊的第二個選項卡) - >程序參數

2

在程序中添加此行接受來自控制檯用戶的輸入:

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

然後添加in.readLine()旁邊的任何變量您想接受運行時輸入。比方說,你要計數變量初始化爲值1,那麼,它應該寫成

int count = in.readLine(); 值1,應在控制檯運行程序

+0

以上解決方案只是在運行時接受用戶輸入的另一種方式,是不是上面貼的問題的答案。請注意 – 2012-07-20 11:19:23

0

這裏後輸入是一個辦法在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);} 
0

這將打印由用戶輸入的號碼:

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