我不知道你意味着將它們放入2D boolean
陣列。如果你在解析文本時遇到問題,那麼這裏有一些你可以使用的代碼片段。
對於第一種解決方案,我假設數字總是成對出現,並由一個單獨的-
分開,空格用於在數字對之間進行分隔。
如果從標準輸入讀取使用此:
Scanner scanner = new Scanner(System.in);
使用這個,如果你有一個String
包含了所有的數字和要處理它(讓變量的名稱是inputString
):
Scanner scanner = new Scanner(inputString);
然後你可以從輸入讀取的數字:
while (scanner.hasNext()) {
String pair = scanner.next();
// Split by the hyphen
String tokens[] = pair.split("-");
// Normally, one should check the array before accessing it
// I currently assume the input is valid
int first = Integer.parseInt(tokens[0]);
int second = Integer.parseInt(tokens[1]);
// Do whatever you want with the 2 numbers extracted
}
另外,您還可以使用useDelimiter添加-
作爲分隔符,並使用nextInt
閱讀次數,而不必單獨處理-
:
scanner.useDelimiter("[\\p{javaWhitespace}-]+");
的代碼將簡單地變爲:
scanner.useDelimiter("[\\p{javaWhitespace}-]+");
while (scanner.hasNext()) {
int first = scanner.nextInt();
int second = scanner.nextInt();
// Do whatever you want with the 2 numbers extracted
}
代碼更清晰,但-
是否出現在first
和second
之間,或出現了多少個-
o r是這兩個數字之間唯一不是現在不知道的字符。如果我們假設輸入格式是正確的,那就可以了。但是,如果沒有給出這個假設,我們就不能做任何輸入驗證。
[你嘗試過什麼?](http://www.whathaveyoutried.com/) –
順便說一句 - 以什麼方式在諸如'11'的整數中轉換爲'boolean'? –
我最好的猜測是每個0-1或0-2是否都是2d數組中的一個位置?那麼這些立場是否屬實?但一些澄清會很好。 – Clark