如何在 java 中将 0 整数变成空白?
How to turn a 0 integer into blank in java?
我是 Java 的新手,目前正在从事一个小型初学者项目。我应该构建一个扫雷 Java 游戏。但是,我一直在寻找将游戏板上的整数“0”更改为空白 space。也许你们可以帮我解决这个问题。谢谢
public class Game {
private Gameloop gameloop;
boolean finish = false;
boolean win = false;
int turn=0;
public Game(){
gameloop = new Gameloop();
Play(gameloop);
}
public void Play(Gameloop gameloop){
do{
turn++;
System.out.println("There are a total of 10 mines in the mine field");
gameloop.show();
finish = gameloop.checkLocation();
if(!finish){
gameloop.numberOfMines();
finish = gameloop.win();
}
}while(!finish);
if(gameloop.win()){
System.out.println("You Win!");
gameloop.showMines();
} else {
System.out.println("Game Over!");
gameloop.showMines();
}
}
public class Gameloop {
private int[][] mines;
private char[][] boardgame;
private int Line, Column;
Random random = new Random();
Scanner input = new Scanner(System.in);
public Gameloop (){
mines = new int[12][12];
boardgame = new char[12][12];
startMines();
randomFillGrid();
fillTips();
startBoard();
}
public boolean win(){
int count=0;
for(int line = 1 ; line < 11 ; line++)
for(int column = 1 ; column < 11 ; column++)
if(boardgame[line][column]=='_')
count++;
if(count == 12)
return true;
else
return false;
}
public void numberOfMines(){
for(int i=-1 ; i<2 ; i++)
for(int j=-1 ; j<2 ; j++)
if( (mines[Line+i][Column+j] != -1) && (Line != 0 && Line != 11 && Column != 0 && Column != 11) )
boardgame[Line+i][Column+j]=Character.forDigit(mines[Line+i][Column+j], 12);
}
public int getPosition(int Line, int Column){
return mines[Line][Column];
}
public boolean checkLocation(){
do{
System.out.println("Enter the row number then the column number");
System.out.print("");
Line = input.nextInt();
System.out.print("");
Column = input.nextInt();
if( (boardgame[Line][Column] != '*') && ((Line < 11 && Line > 0) && (Column < 11 && Column > 0)))
System.out.println("Field already shown");
if( Line < 1 || Line > 10 || Column < 1 || Column > 10)
System.out.println("Choose a number between 1 and 10");
}while((Line < 1 || Line > 10 || Column < 1 || Column > 10) || (boardgame[Line][Column] != '.') );
if(getPosition(Line, Column)== -1)
return true;
else
return false;
}
public void show(){
for(int Line = 10 ; Line > 0 ; Line--){
System.out.print(" "+Line + " ");
for(int Column = 1 ; Column < 11 ; Column++){
System.out.print(" "+ boardgame[Line][Column]);
}
System.out.println();
}
System.out.println("\n 1 2 3 4 5 6 7 8 9 10 ");
}
public void fillTips(){
for(int line=1 ; line < 11 ; line++)
for(int column=1 ; column < 11 ; column++){
for(int i=-1 ; i<=1 ; i++)
for(int j=-1 ; j<=1 ; j++)
if(mines[line][column] != -1)
if(mines[line+i][column+j] == -1)
mines[line][column]++;
}
}
public void showMines(){
for(int i=1 ; i < 11; i++)
for(int j=1 ; j < 11 ; j++)
if(mines[i][j] == -1)
boardgame[i][j]='*';
show();
}
public void startBoard(){
for(int i=1 ; i<mines.length ; i++)
for(int j=1 ; j<mines.length ; j++)
boardgame[i][j]= '.';
}
public void startMines(){
for(int i=0 ; i<mines.length ; i++)
for(int j=0 ; j<mines.length ; j++)
mines[i][j]= 0;
}
public void randomFillGrid(){
boolean raffled;
int Line, Column;
for(int i=0 ; i<12 ; i++){
do{
Line = random.nextInt(10) + 1;
Column = random.nextInt(10) + 1;
if(mines[Line][Column] == -1)
raffled=true;
else
raffled = false;
}while(raffled);
mines[Line][Column] = -1;
}
}
}
}
This is what my board looks like. I want to change the zeros for blank so it looks similar to the second board example.
10 . . . . . . . . . .
9 . . . . . . . . . .
8 . . . . . . . . . .
7 . . . . . . . . . .
6 . . . . . . 2 1 . .
5 . . . . . . 1 1 1 .
4 . . . . . . 0 0 0 .
3 . . . . . . . . . .
2 1 . 2 . . . . . . .
1 1 1 1 . . . . . . .
1 2 3 4 5 6 7 8 9 10
**Second example:**
10 . . . . . . . . . .
9 . . . . . . . . . .
8 . . . . . . . . . .
7 . . . . . . . . . .
6 . . . . . . 2 1 . .
5 . . . . . . 1 1 1 .
4 . . . . . . . .
3 . . . . . . . . . .
2 1 . 2 . . . . . . .
1 1 1 1 . . . . . . .
1 2 3 4 5 6 7 8 9 10
int值中没有空格。所以最简单的方法是写一个returnes一个char或string的方法。它应该 return 数字本身作为 char 或字符串(转换时要小心,数字可能无法用简单的转换正确解释,可能有 .toChar 或类似的东西)或 ' ' 如果它是 0
改变
boardgame[Line+i][Column+j]=Character.forDigit(mines[Line+i][Column+j], 12);
进入
char ch = Character.forDigit(mines[Line+i][Column+j], 12); // 12 digits 0, 1, .. 9, a, b
boardgame[Line+i][Column+j] = ch == '0' ? ' ' : ch;
而不是:
private int[][] mines;
为什么不使用:
private char[][] mines;
mines[i][j]=' ';
None 到目前为止我知道,唯一的方法是进行逻辑检查,如果值为 0,则显示空 space。
如果你需要用字符表示,你可以使用这个。
Character ch = Character.MIN_VALUE;
System.out.print(" "+ (boardgame[Line][Column] == '0' ? "" : boardgame[Line][Column]));
用上面的行替换 show
方法中的打印行。您将获得所需的结果。
我是 Java 的新手,目前正在从事一个小型初学者项目。我应该构建一个扫雷 Java 游戏。但是,我一直在寻找将游戏板上的整数“0”更改为空白 space。也许你们可以帮我解决这个问题。谢谢
public class Game {
private Gameloop gameloop;
boolean finish = false;
boolean win = false;
int turn=0;
public Game(){
gameloop = new Gameloop();
Play(gameloop);
}
public void Play(Gameloop gameloop){
do{
turn++;
System.out.println("There are a total of 10 mines in the mine field");
gameloop.show();
finish = gameloop.checkLocation();
if(!finish){
gameloop.numberOfMines();
finish = gameloop.win();
}
}while(!finish);
if(gameloop.win()){
System.out.println("You Win!");
gameloop.showMines();
} else {
System.out.println("Game Over!");
gameloop.showMines();
}
}
public class Gameloop {
private int[][] mines;
private char[][] boardgame;
private int Line, Column;
Random random = new Random();
Scanner input = new Scanner(System.in);
public Gameloop (){
mines = new int[12][12];
boardgame = new char[12][12];
startMines();
randomFillGrid();
fillTips();
startBoard();
}
public boolean win(){
int count=0;
for(int line = 1 ; line < 11 ; line++)
for(int column = 1 ; column < 11 ; column++)
if(boardgame[line][column]=='_')
count++;
if(count == 12)
return true;
else
return false;
}
public void numberOfMines(){
for(int i=-1 ; i<2 ; i++)
for(int j=-1 ; j<2 ; j++)
if( (mines[Line+i][Column+j] != -1) && (Line != 0 && Line != 11 && Column != 0 && Column != 11) )
boardgame[Line+i][Column+j]=Character.forDigit(mines[Line+i][Column+j], 12);
}
public int getPosition(int Line, int Column){
return mines[Line][Column];
}
public boolean checkLocation(){
do{
System.out.println("Enter the row number then the column number");
System.out.print("");
Line = input.nextInt();
System.out.print("");
Column = input.nextInt();
if( (boardgame[Line][Column] != '*') && ((Line < 11 && Line > 0) && (Column < 11 && Column > 0)))
System.out.println("Field already shown");
if( Line < 1 || Line > 10 || Column < 1 || Column > 10)
System.out.println("Choose a number between 1 and 10");
}while((Line < 1 || Line > 10 || Column < 1 || Column > 10) || (boardgame[Line][Column] != '.') );
if(getPosition(Line, Column)== -1)
return true;
else
return false;
}
public void show(){
for(int Line = 10 ; Line > 0 ; Line--){
System.out.print(" "+Line + " ");
for(int Column = 1 ; Column < 11 ; Column++){
System.out.print(" "+ boardgame[Line][Column]);
}
System.out.println();
}
System.out.println("\n 1 2 3 4 5 6 7 8 9 10 ");
}
public void fillTips(){
for(int line=1 ; line < 11 ; line++)
for(int column=1 ; column < 11 ; column++){
for(int i=-1 ; i<=1 ; i++)
for(int j=-1 ; j<=1 ; j++)
if(mines[line][column] != -1)
if(mines[line+i][column+j] == -1)
mines[line][column]++;
}
}
public void showMines(){
for(int i=1 ; i < 11; i++)
for(int j=1 ; j < 11 ; j++)
if(mines[i][j] == -1)
boardgame[i][j]='*';
show();
}
public void startBoard(){
for(int i=1 ; i<mines.length ; i++)
for(int j=1 ; j<mines.length ; j++)
boardgame[i][j]= '.';
}
public void startMines(){
for(int i=0 ; i<mines.length ; i++)
for(int j=0 ; j<mines.length ; j++)
mines[i][j]= 0;
}
public void randomFillGrid(){
boolean raffled;
int Line, Column;
for(int i=0 ; i<12 ; i++){
do{
Line = random.nextInt(10) + 1;
Column = random.nextInt(10) + 1;
if(mines[Line][Column] == -1)
raffled=true;
else
raffled = false;
}while(raffled);
mines[Line][Column] = -1;
}
}
}
}
This is what my board looks like. I want to change the zeros for blank so it looks similar to the second board example.
10 . . . . . . . . . .
9 . . . . . . . . . .
8 . . . . . . . . . .
7 . . . . . . . . . .
6 . . . . . . 2 1 . .
5 . . . . . . 1 1 1 .
4 . . . . . . 0 0 0 .
3 . . . . . . . . . .
2 1 . 2 . . . . . . .
1 1 1 1 . . . . . . .
1 2 3 4 5 6 7 8 9 10
**Second example:**
10 . . . . . . . . . .
9 . . . . . . . . . .
8 . . . . . . . . . .
7 . . . . . . . . . .
6 . . . . . . 2 1 . .
5 . . . . . . 1 1 1 .
4 . . . . . . . .
3 . . . . . . . . . .
2 1 . 2 . . . . . . .
1 1 1 1 . . . . . . .
1 2 3 4 5 6 7 8 9 10
int值中没有空格。所以最简单的方法是写一个returnes一个char或string的方法。它应该 return 数字本身作为 char 或字符串(转换时要小心,数字可能无法用简单的转换正确解释,可能有 .toChar 或类似的东西)或 ' ' 如果它是 0
改变
boardgame[Line+i][Column+j]=Character.forDigit(mines[Line+i][Column+j], 12);
进入
char ch = Character.forDigit(mines[Line+i][Column+j], 12); // 12 digits 0, 1, .. 9, a, b
boardgame[Line+i][Column+j] = ch == '0' ? ' ' : ch;
而不是:
private int[][] mines;
为什么不使用:
private char[][] mines;
mines[i][j]=' ';
None 到目前为止我知道,唯一的方法是进行逻辑检查,如果值为 0,则显示空 space。
如果你需要用字符表示,你可以使用这个。
Character ch = Character.MIN_VALUE;
System.out.print(" "+ (boardgame[Line][Column] == '0' ? "" : boardgame[Line][Column]));
用上面的行替换 show
方法中的打印行。您将获得所需的结果。