下面我有一个井字游戏程序的完整代码副本。我知道它还不多,但我被困在获取输入的部分。我已经设法从用户那里获得了1个输入,然后将其打印出来(列),但当我试图为行输入不同的内容时,它会为我提供用于列的任何内容。有什么想法吗?
我只是在学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);
}
}
更改
System.out.print ("Your row is " + columnGotten);
至
System.out.print ("Your row is " + rowGotten);
尝试使用Scanner进行输入。
Scanner sc = new Scanner();
int x = sc.nextInt();
String s = sc.nextLine();
等等。希望它能有所帮助。