java中如何在2D数组中插入值
如何使用2D数组填充3x3矩阵,以便用户选择什么 要输入字符串值的数组的位置 职位格式为:java中如何在2D数组中插入值,java,arrays,2d,Java,Arrays,2d,如何使用2D数组填充3x3矩阵,以便用户选择什么 要输入字符串值的数组的位置 职位格式为:(行号、列号) 例如: Person 1, please pick where to place an X: (0,1) Person 2, please pick where to place an O: (0,2) 这就是我所拥有的: import java.util.Scanner; public class idk { public static void main(String[] a
(行号、列号)
例如:
Person 1, please pick where to place an X: (0,1)
Person 2, please pick where to place an O: (0,2)
这就是我所拥有的:
import java.util.Scanner;
public class idk {
public static void main(String[] args)
{
int i;
int j;
int arr[][] = new int[3][3];
// Getting user input
Scanner input = new Scanner(System.in);
System.out.println("Enter a number: ");
for (i = 0; i < 3; i++) {
for (j = 0; j < 3; j++) {
arr[i][j] = input.nextInt();
}
}
// Outputting the user input
System.out.println("The output is: ");
for (i = 0; i < 3; i++) {
for (j = 0; j < 3; j++) {
System.out.printf("%d ", arr[i][j]);
}
}
}
}
import java.util.Scanner;
公共类idk{
公共静态void main(字符串[]args)
{
int i;
int j;
int arr[][]=新int[3][3];
//获取用户输入
扫描仪输入=新扫描仪(System.in);
System.out.println(“输入一个数字:”);
对于(i=0;i<3;i++){
对于(j=0;j<3;j++){
arr[i][j]=input.nextInt();
}
}
//输出用户输入
System.out.println(“输出为:”);
对于(i=0;i<3;i++){
对于(j=0;j<3;j++){
System.out.printf(“%d”,arr[i][j]);
}
}
}
}
简单
arr[Row Number][Column Number]=X;
乙二醇
或
但因为它是一个整数数组,所以不能在其中放置字符串,即“O”和“X”
尝试将其设置为字符串数组类似于以下内容的内容具有您想要的大部分部分,但错误处理除外。输入是数字、空格(或任何空格),最后是另一个数字。输入数字包括1到3,这是正常人所期望的
import java.util.Scanner;
public class TicTacToe {
char board[][] = new char[][]{{'-','-','-'},{'-','-','-'},{'-','-','-'}};
public static void main(String[] args) {
TicTacToe ttt = new TicTacToe();
ttt.run();
}
public void run() {
Scanner input = new Scanner(System.in);
int row = -1, col = -1;//just to initialize
char symbol = 'o';
while (true) {
symbol = (symbol == 'x')?'o':'x';
boolean error = false;
System.out.println("Enter a number: ");
if (input.hasNext()) {
row = input.nextInt();
System.out.println("row: " + row);
} else {
error = true;
}
if (input.hasNext()) {
col = input.nextInt();
System.out.println("col: " + col);
} else {
error = true;
}
if (!error) {
board[row - 1][col - 1] = symbol;
}else{
System.out.println("an error has occurred");
}
input.reset();
this.drawBoard();
}
}
public void drawBoard() {
System.out.println("The output is: ");
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
System.out.printf("%c ", board[i][j]);
}
System.out.println();
}
System.out.println();
}
}
import java.util.Scanner;
公共类Tictatcoe{
字符板[][]=新字符[][]{{'-'、'-'、'-'}、{'-'、'-'、'-'}、{'-'、'-'、'-'、'-'};
公共静态void main(字符串[]args){
TicTacToe ttt=新的TicTacToe();
ttt.run();
}
公开募捐{
扫描仪输入=新扫描仪(System.in);
int row=-1,col=-1;//仅用于初始化
字符符号='o';
while(true){
符号=(符号='x')?'o':'x';
布尔错误=假;
System.out.println(“输入一个数字:”);
if(input.hasNext()){
row=input.nextInt();
System.out.println(“行:”+行);
}否则{
错误=真;
}
if(input.hasNext()){
col=input.nextInt();
System.out.println(“col:+col”);
}否则{
错误=真;
}
如果(!错误){
线路板[第1行][第1列]=符号;
}否则{
System.out.println(“发生错误”);
}
input.reset();
这是一个抽屉();
}
}
公共真空抽油板(){
System.out.println(“输出为:”);
对于(int i=0;i<3;i++){
对于(int j=0;j<3;j++){
System.out.printf(“%c”,板[i][j]);
}
System.out.println();
}
System.out.println();
}
}
如果你查一下,你会看到一个使用正则表达式解析的例子,我更喜欢这个方法,因为使用正则表达式你可以一次验证整个字符串,但是因为问题代码中没有这个,所以我没有使用这个方法
arr[1][0]=O;
import java.util.Scanner;
public class TicTacToe {
char board[][] = new char[][]{{'-','-','-'},{'-','-','-'},{'-','-','-'}};
public static void main(String[] args) {
TicTacToe ttt = new TicTacToe();
ttt.run();
}
public void run() {
Scanner input = new Scanner(System.in);
int row = -1, col = -1;//just to initialize
char symbol = 'o';
while (true) {
symbol = (symbol == 'x')?'o':'x';
boolean error = false;
System.out.println("Enter a number: ");
if (input.hasNext()) {
row = input.nextInt();
System.out.println("row: " + row);
} else {
error = true;
}
if (input.hasNext()) {
col = input.nextInt();
System.out.println("col: " + col);
} else {
error = true;
}
if (!error) {
board[row - 1][col - 1] = symbol;
}else{
System.out.println("an error has occurred");
}
input.reset();
this.drawBoard();
}
}
public void drawBoard() {
System.out.println("The output is: ");
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
System.out.printf("%c ", board[i][j]);
}
System.out.println();
}
System.out.println();
}
}