java printf输出boolean
时间: 2023-11-12 08:50:25 浏览: 57
在Java中,printf方法不能直接输出boolean类型的值。如果要输出boolean值,可以使用格式化字符串来实现。可以将boolean值转换为字符串,然后使用%s格式化字符串输出。例如:
boolean flag = true;
System.out.printf("flag的值为%s", flag ? "true" : "false");
相关问题
JAVA中printf怎么用
在Java中,`printf`是一种格式化输出的方法。它可以将数据格式化为指定的格式并输出到控制台或其他输出流中。
下面是一些`printf`的使用示例:
1. 输出字符串
```
String name = "Alice";
System.out.printf("Hello, %s!\n", name);
```
输出结果为:
```
Hello, Alice!
```
在上面的代码中,`%s`表示字符串类型的占位符,用于将变量`name`的值格式化为字符串并输出。
2. 输出整数
```
int x = 123;
System.out.printf("x = %d\n", x);
```
输出结果为:
```
x = 123
```
在上面的代码中,`%d`表示整数类型的占位符,用于将变量`x`的值格式化为整数并输出。
3. 输出浮点数
```
double pi = 3.141592653589793;
System.out.printf("pi = %.2f\n", pi);
```
输出结果为:
```
pi = 3.14
```
在上面的代码中,`%.2f`表示浮点数类型的占位符,并指定小数点后保留2位小数。这将变量`pi`的值格式化为浮点数并输出。
4. 输出布尔值
```
boolean flag = true;
System.out.printf("flag = %b\n", flag);
```
输出结果为:
```
flag = true
```
在上面的代码中,`%b`表示布尔类型的占位符,用于将变量`flag`的值格式化为布尔值并输出。
除了上面的示例,`printf`还支持许多其他的占位符和格式化选项,如日期、时间、十六进制数等。可以参考Java官方文档中的格式化字符串语法来了解更多内容。
java输出五子棋程序
好的,我可以为您分享一个基于Java编写的五子棋程序。以下为代码:
```
import java.util.Scanner;
public class GobangGame {
private static char[][] board;
private static final int ROW = 15;
private static final int COL = 15;
private static char playerOne = 'X';
private static char playerTwo = 'O';
public static void main(String[] args) {
initBoard();
printBoard();
Scanner scanner = new Scanner(System.in);
int x, y;
char currentPlayer = playerOne;
while (true) {
System.out.printf("Player %s, please enter the coordinate row and column you want to place the chess piece(separated by space):", currentPlayer);
x = scanner.nextInt();
y = scanner.nextInt();
if (isValid(x, y)) {
board[x][y] = currentPlayer;
printBoard();
if (isWin(x, y)) {
System.out.printf("Congratulations! Player %s won the game!", currentPlayer);
break;
}
if (isFull()) {
System.out.println("The game ended in a draw!");
break;
}
currentPlayer = currentPlayer == playerOne ? playerTwo : playerOne;
} else {
System.out.println("Illegal move, please try again.");
}
}
}
public static void initBoard() {
board = new char[ROW][COL];
for (int i = 0; i < ROW; i++) {
for (int j = 0; j < COL; j++) {
board[i][j] = '+';
}
}
}
public static void printBoard() {
for (int i = 0; i < ROW; i++) {
for (int j = 0; j < COL; j++) {
System.out.print(board[i][j] + " ");
}
System.out.println();
}
}
public static boolean isValid(int x, int y) {
if (x < 0 || x >= ROW || y < 0 || y >= COL) {
return false;
}
if (board[x][y] != '+') {
return false;
}
return true;
}
public static boolean isWin(int x, int y) {
char current = board[x][y];
int count = 1; // count for horizontal
int i, j;
// check horizontal
i = x;
j = y - 1;
while (j >= 0 && board[i][j] == current) {
count++;
j--;
}
j = y + 1;
while (j < COL && board[i][j] == current) {
count++;
j++;
}
if (count >= 5) {
return true;
}
// check vertical
count = 1;
i = x - 1;
j = y;
while (i >= 0 && board[i][j] == current) {
count++;
i--;
}
i = x + 1;
while (i < ROW && board[i][j] == current) {
count++;
i++;
}
if (count >= 5) {
return true;
}
// check diagonal
count = 1;
i = x - 1;
j = y - 1;
while (i >= 0 && j >= 0 && board[i][j] == current) {
count++;
i--;
j--;
}
i = x + 1;
j = y + 1;
while (i < ROW && j < COL && board[i][j] == current) {
count++;
i++;
j++;
}
if (count >= 5) {
return true;
}
// check anti-diagonal
count = 1;
i = x - 1;
j = y + 1;
while (i >= 0 && j < COL && board[i][j] == current) {
count++;
i--;
j++;
}
i = x + 1;
j = y - 1;
while (i < ROW && j >= 0 && board[i][j] == current) {
count++;
i++;
j--;
}
if (count >= 5) {
return true;
}
return false;
}
public static boolean isFull() {
for (int i = 0; i < ROW; i++) {
for (int j = 0; j < COL; j++) {
if (board[i][j] == '+') {
return false;
}
}
}
return true;
}
}
```
希望这个程序能够帮助您学习五子棋的编写。如果还有其他问题,可以随时向我提出。
相关推荐
![pptx](https://img-home.csdnimg.cn/images/20210720083543.png)
![ppt](https://img-home.csdnimg.cn/images/20210720083527.png)
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)