Java从0开始——输入输出

Java从0开始——输入输出

输出

System.out.print();//直接打印
System.out.println();//打印后换行,当括号里为空,只进行换行

输入
是否使用包,有两种格式可写:

  • 使用包
import java.util.*;//首先导入包

Scanner reader = new Scanner (System.in);
int x = reader.nextInt();
  • 直接写
java.util.Scanner s = new java.util.Scanner(System.in);
int x = s.nextInt();
  • 基本数据类型

nextBoolean(),nextByte(),nextShort(),nextInt(),nextLong(),nextFloat(),nextDouble()

  • 引用数据类型(字符串)
String str = s.nextLine();
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是一个简单的扫雷小游戏的 JAVA 代码,实现了基本的游戏功能: ``` import java.util.Random; import java.util.Scanner; public class Minesweeper { private static final int ROWS = 10; private static final int COLS = 10; private static final int BOMBS = 10; private static final char COVERED = '-'; private static final char UNCOVERED = ' '; private static final char BOMB = '*'; private char[][] board; private boolean[][] bombPositions; public Minesweeper() { board = new char[ROWS][COLS]; bombPositions = new boolean[ROWS][COLS]; initBoard(); initBombs(); } private void initBoard() { for (int i = 0; i < ROWS; i++) { for (int j = 0; j < COLS; j++) { board[i][j] = COVERED; } } } private void initBombs() { Random random = new Random(); int count = 0; while (count < BOMBS) { int row = random.nextInt(ROWS); int col = random.nextInt(COLS); if (!bombPositions[row][col]) { bombPositions[row][col] = true; count++; } } } private int countAdjacentBombs(int row, int col) { int count = 0; for (int i = row - 1; i <= row + 1; i++) { for (int j = col - 1; j <= col + 1; j++) { if (i >= 0 && i < ROWS && j >= 0 && j < COLS && bombPositions[i][j]) { count++; } } } return count; } public boolean playMove(int row, int col) { if (bombPositions[row][col]) { board[row][col] = BOMB; return false; } int adjacentBombs = countAdjacentBombs(row, col); board[row][col] = (char) ('0' + adjacentBombs); if (adjacentBombs == 0) { for (int i = row - 1; i <= row + 1; i++) { for (int j = col - 1; j <= col + 1; j++) { if (i >= 0 && i < ROWS && j >= 0 && j < COLS && board[i][j] == COVERED) { playMove(i, j); } } } } return true; } public void playGame() { Scanner scanner = new Scanner(System.in); boolean gameOver = false; while (!gameOver) { printBoard(); System.out.print("Enter row and column (e.g. 1 2): "); int row = scanner.nextInt() - 1; int col = scanner.nextInt() - 1; if (row < 0 || row >= ROWS || col < 0 || col >= COLS) { System.out.println("Invalid row or column!"); continue; } gameOver = !playMove(row, col); if (checkWin()) { System.out.println("You win!"); gameOver = true; } } printBoard(); } private boolean checkWin() { for (int i = 0; i < ROWS; i++) { for (int j = 0; j < COLS; j++) { if (board[i][j] == COVERED && !bombPositions[i][j]) { return false; } } } return true; } private void printBoard() { System.out.print(" "); for (int j = 0; j < COLS; j++) { System.out.print((j + 1) + " "); } System.out.println(); for (int i = 0; i < ROWS; i++) { System.out.print((i + 1) + " "); for (int j = 0; j < COLS; j++) { System.out.print(board[i][j] == UNCOVERED && bombPositions[i][j] ? BOMB : board[i][j]); System.out.print(" "); } System.out.println(); } } public static void main(String[] args) { Minesweeper minesweeper = new Minesweeper(); minesweeper.playGame(); } } ``` 代码中定义了游戏的行,列和雷,以及用来表示板块状态的字符常量。在 `Minesweeper` 类中,通过二维组 `board` 和 `bombPositions` 来表示游戏面板和雷的位置。通过 `initBoard` 和 `initBombs` 方法初始化游戏面板和雷的位置。 在 `countAdjacentBombs` 方法中,根据当前位置的周围八个位置计算雷的个。 在 `playMove` 方法中,根据当前位置是否有雷计算该位置的状态,并根据周围是否有雷递归更新周围的位置状态。 在 `playGame` 方法中,通过 `Scanner` 类读取玩家的输入,并根据输入更新游戏状态,直到游戏结束。 在 `checkWin` 方法中,检查游戏是否胜利。 在 `printBoard` 方法中,输出游戏面板。 在 `main` 方法中,创建 `Minesweeper` 对象并调用 `playGame` 方法开始游戏。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值