下面我有一個我的完整代碼的井字遊戲程序的副本。我知道它還沒有太多,但我被困在獲取輸入部分。我已經設法從用戶那裏得到1個輸入,然後將其輸出(列),但是當我嘗試爲行輸入不同的東西時,它給了我用於列的任何內容。有關如何解決它的任何想法?如何從Java(控制檯)中的用戶獲取輸入?
我剛剛學習java,請溫柔。
import java.io.*;
public class Main {
public static void main(String[] args) throws IOException {
System.out.println ("Please make your first move by entering a column and then a row, like this: c r \n");
int columnGotten = 0;
int rowGotten = 0;
//gets your column number choice
BufferedReader columnInput = new BufferedReader(new InputStreamReader (System.in));
try {
columnGotten = Integer.parseInt(columnInput.readLine());
} catch (NumberFormatException nfe) {
System.out.println ("If you're not going to play fair, I'm going to leave. Bye.");
return;
}
System.out.print ("Your column is " + columnGotten + "\n");
//gets your row number choice
BufferedReader rowInput = new BufferedReader(new InputStreamReader (System.in));
try {
rowGotten = Integer.parseInt(rowInput.readLine());
} catch (NumberFormatException nfe) {
System.out.println ("If you're not going to play fair, I'm going to leave. Bye.");
return;
}
System.out.print ("Your row is " + columnGotten);
}
}
您不得建立新的閱讀器,只需使用前面的閱讀器。 – 2012-03-11 22:59:52
我試圖做到這一點,但它仍然給了我相同的錯誤(顯示舊的輸入)。 – Vasu 2012-03-11 23:01:06