2010-04-05 149 views
1

大家好我試圖通過解決從ACM的一些問題,以提高我的Java技能,現在這東西是我的樣本輸入如下所示:閱讀輸入

3 100 
34 100 
75 250 
27 2147483647 
101 304 
101 303 
-1 -1 

所以剛開始我只是想讀他們,但它不是在這裏工作是Java代碼:從文件中讀取它們

import java.io.BufferedInputStream; 
import java.util.Scanner; 

public class Main { 

    public static void main(String args[]) { 
     Scanner stdin = new Scanner(new BufferedInputStream(System.in)); 
     while (stdin.hasNext()) { 
      System.out.println(stdin.nextInt() + " and the next " + stdin.nextInt()); 
     } 
    } 
} 

我想送這些輸入作爲參數,而不是,這裏是如何:

alt text http://i41.tinypic.com/j61npu.gif

該程序只是旋轉(執行)但不打印任何東西。我怎樣才能解決這個問題?

回答

5

您在嚮導中指定的參數不是通過std in輸入到程序中的,而是通過傳遞到main方法的參數String[] args傳入的。由於看起來好像您在Eclipse中運行,您可以轉到生成的控制檯選項卡並鍵入該窗口。或者,您可以遍歷args param來獲取這些值。

0

試試這個:

Scanner sc = new Scanner (System.in); 
while (sc.hasNextInt()) { 
    System.out.println(sc.nextInt()); 
} 

之後你開始你應該提供一些數據作爲輸入的程序。

1

我沒有使用Eclipse,但是這應該會打印輸入的數字。 你傳入的數字作爲字符串參數,所以這樣的:「3 100」實際上是等於

String str = 3 + " " + 100; 

這應該打印的數字:

public static void main(String[] args) 
{ 
    for (String arg : args) 
    { 
     System.out.println(arg); 
    } 
}