我要做的是生成一个15x15的“ - ”方格并接受一个用户输入坐标,然后将“ - ”替换为“x”,目前我的程序只是打印一条垂直线“ - ” “
import java.util.*;
public class GameOfLife
{
public static void main(String[] args)
{
int[][] boardList = new int[15][15];
Scanner myScanner = new Scanner(System.in);
boolean done = false;
do
{
System.out.println("1 - Add a being \n 2 - Show current board \n 3 - Show next generation \n 4 - Clear board \n 5 - Add preload pattern \n 6 - Exit");
int choice = Integer.parseInt(myScanner.nextLine());
if (choice == 1)
{
System.out.print("Enter the x coordinate: ");
String answer = myScanner.nextLine();
int xCoordinate = Integer.parseInt(answer);
System.out.print("Enter the y coordinate: ");
String answer2 = myScanner.nextLine();
int yCoordinate = Integer.parseInt(answer2);
for(int i = 0; i < 15; i++)
{
for(int j = 0; j < 15; j++)
{
if(xCoordinate == i)
{
if(yCoordinate == j)
{
System.out.printf("x", boardList[i][j]);
}
}
else
System.out.printf("-", boardList[i][j]);
System.out.println();
}
}
}
}
}
}
答案 0 :(得分:2)
在这里,你的工作......你需要把System.out.println();
放在内部循环之外以及放置
if(xCoordinate == i){
if(yCoordinate == j){
一个条件......
public static void main(String[] args) {
int[][] boardList = new int[15][15];
Scanner myScanner = new Scanner(System.in);
boolean done = true;
do {
System.out
.println("1 - Add a being \n 2 - Show current board \n 3 - Show next generation \n 4 - Clear board \n 5 - Add preload pattern \n 6 - Exit");
int choice = Integer.parseInt(myScanner.nextLine());
if (choice == 1) {
System.out.print("Enter the x coordinate: ");
String answer = myScanner.nextLine();
int xCoordinate = Integer.parseInt(answer);
System.out.print("Enter the y coordinate: ");
String answer2 = myScanner.nextLine();
int yCoordinate = Integer.parseInt(answer2);
for (int i = 0; i < 15; i++) {
for (int j = 0; j < 15; j++) {
if (xCoordinate == i && yCoordinate == j) {
System.out.printf("x", boardList[i][j]);
} else
System.out.printf("-", boardList[i][j]);
}
System.out.println();
}
}
} while (done);
}
//编辑请注意,我将done
更改为true只是为了证明它有效...
答案 1 :(得分:1)
试试这个
for(int i = 0; i < 15; i++)
{
for(int j = 0; j < 15; j++)
{
if(xCoordinate == i && yCoordinate==j)
System.out.printf("x", boardList[i][j]);
else
System.out.printf("-", boardList[i][j]);
}
System.out.println();
}
首先完成整行打印后,需要打印新行
答案 2 :(得分:0)
如果我理解得很好,你想要一个用' - '初始化的二维数组,你可以这样做
int[][] boardList = new int[15][15];
for (int row = 0; row < 15; row ++)
for (int col = 0; col < 15; col++)
boardList[row][col] = '-';
然后,一旦您将用户数据存储在xCoordinate
和Ycoordinate
中,您就可以了:
boardList[xCoordinate][Ycoordinate] = 'x';
答案 3 :(得分:0)
您可以尝试以下代码:
import java.util.Scanner;
public class StackOverflow
{
public static void main(String[] args)
{
int[][] boardList = new int[15][15];
Scanner myScanner = new Scanner(System.in);
boolean done = false;
System.out.println("1 - Add a being \n 2 - Show current board \n 3 - Show next generation \n 4 - Clear board \n 5 - Add preload pattern \n 6 - Exit");
int choice = Integer.parseInt(myScanner.nextLine());
if (choice == 1)
{
System.out.print("Enter the x coordinate: ");
String answer = myScanner.nextLine();
int xCoordinate = Integer.parseInt(answer);
System.out.print("Enter the y coordinate: ");
String answer2 = myScanner.nextLine();
int yCoordinate = Integer.parseInt(answer2);
for(int i = 0; i < 15; i++)
{
for(int j = 0; j < 15; j++)
{
if(xCoordinate == i)
{
if(yCoordinate == j)
{
System.out.printf("x", boardList[i][j]);
}
else
{
System.out.printf("-", boardList[i][j]);
}
}
else
{
System.out.printf("-", boardList[i][j]);
}
}
System.out.println();
}
}
}
}