卧薪尝胆之JAVA重学(JAVA程序运行机制)

计算机高级语言有主要有两种:
1、编译型
2、解释行

JAVA是包含两种语言的结合

JAVA程序 运行流程 :

.java 源文件  →  Java编译器 → 字节码文件 (.class) → JVM → 操作系统 

JAVA编译器  编译.JAVA源文件  

JVM (解释字节码文件): 类装载器 → 字节码校验器  → 解释器  

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是一个简单的成语消消乐Java代码,仅供参考: ```java import java.util.ArrayList; import java.util.List; import java.util.Scanner; public class ChengYuXiaoXiaoLe { // 成语库 private static final String[] CHENGYUS = {"卧薪尝胆", "金蝉脱壳", "百里挑一", "金玉满堂", "背水一战", "霸王别姬", "天上人间", "不吐不快", "海阔天空", "情非得已"}; // 游戏面板大小 private static final int ROWS = 10; private static final int COLS = 10; // 游戏面板数据 private static final char[][] BOARD = new char[ROWS][COLS]; // 记录已选择的成语 private static final List<String> SELECTED = new ArrayList<>(); // 记录当前选中的成语的起点和终点位置 private static int startX = -1; private static int startY = -1; private static int endX = -1; private static int endY = -1; // 初始化游戏面板 private static void initBoard() { for (int i = 0; i < ROWS; i++) { for (int j = 0; j < COLS; j++) { BOARD[i][j] = (char) ('a' + (int) (Math.random() * 26)); } } // 将成语插入游戏面板中 for (String chengyu : CHENGYUS) { int len = chengyu.length(); int startX, startY, endX, endY; // 随机选择成语的起点和终点位置 do { startX = (int) (Math.random() * ROWS); startY = (int) (Math.random() * COLS); endX = (int) (Math.random() * ROWS); endY = (int) (Math.random() * COLS); } while (len != Math.abs(startX - endX) + Math.abs(startY - endY)); // 将成语插入游戏面板中 int index = 0; if (startX == endX) { for (int j = Math.min(startY, endY); j <= Math.max(startY, endY); j++) { BOARD[startX][j] = chengyu.charAt(index++); } } else { for (int i = Math.min(startX, endX); i <= Math.max(startX, endX); i++) { BOARD[i][startY] = chengyu.charAt(index++); } } } } // 打印游戏面板 private static void printBoard() { for (int i = 0; i < ROWS; i++) { for (int j = 0; j < COLS; j++) { System.out.print(BOARD[i][j] + " "); } System.out.println(); } } // 判断两个位置是否相邻 private static boolean isAdjacent(int x1, int y1, int x2, int y2) { return Math.abs(x1 - x2) <= 1 && Math.abs(y1 - y2) <= 1; } // 判断当前选中的成语是否合法 private static boolean isValidSelection() { if (startX == -1 || endX == -1) { return false; } StringBuilder sb = new StringBuilder(); if (startX == endX) { for (int j = Math.min(startY, endY); j <= Math.max(startY, endY); j++) { sb.append(BOARD[startX][j]); } } else { for (int i = Math.min(startX, endX); i <= Math.max(startX, endX); i++) { sb.append(BOARD[i][startY]); } } String chengyu = sb.toString(); return chengyu.length() >= 4 && !SELECTED.contains(chengyu) && contains(chengyu); } // 判断指定的成语是否在成语库中 private static boolean contains(String chengyu) { for (String cy : CHENGYUS) { if (cy.equals(chengyu)) { return true; } } return false; } // 处理玩家的点击事件 private static void handleClick(int x, int y) { if (startX == -1) { // 如果当前没有选中成语,将当前位置作为成语的起点 startX = x; startY = y; endX = x; endY = y; } else if (isAdjacent(x, y, endX, endY)) { // 如果当前位置与上一个位置相邻,将当前位置作为成语的终点 endX = x; endY = y; } else { // 如果当前位置与上一个位置不相邻,取消选中成语 startX = -1; startY = -1; endX = -1; endY = -1; } // 判断当前选中的成语是否合法 if (isValidSelection()) { // 如果合法,将成语加入已选择列表中,并将成语从游戏面板中删除 StringBuilder sb = new StringBuilder(); if (startX == endX) { for (int j = Math.min(startY, endY); j <= Math.max(startY, endY); j++) { sb.append(BOARD[startX][j]); BOARD[startX][j] = ' '; } } else { for (int i = Math.min(startX, endX); i <= Math.max(startX, endX); i++) { sb.append(BOARD[i][startY]); BOARD[i][startY] = ' '; } } SELECTED.add(sb.toString()); startX = -1; startY = -1; endX = -1; endY = -1; // 打印当前已选择的成语列表 System.out.println("已选择的成语:"); for (String cy : SELECTED) { System.out.print(cy + " "); } System.out.println(); } } public static void main(String[] args) { initBoard(); printBoard(); Scanner scanner = new Scanner(System.in); while (true) { System.out.print("请输入要选择的位置(x,y): "); String input = scanner.nextLine(); if (input.equals("exit")) { break; } String[] arr = input.split(","); if (arr.length != 2) { System.out.println("输入格式错误,请重新输入!"); continue; } try { int x = Integer.parseInt(arr[0]); int y = Integer.parseInt(arr[1]); if (x < 0 || x >= ROWS || y < 0 || y >= COLS) { System.out.println("输入位置不合法,请重新输入!"); continue; } handleClick(x, y); printBoard(); } catch (NumberFormatException e) { System.out.println("输入格式错误,请重新输入!"); } } } } ``` 该代码实现了一个简单的成语消消乐游戏,其中包含了成语库的定义、游戏面板的初始化、游戏面板的打印、玩家点击事件的处理等功能。玩家通过输入坐标来选择游戏面板上的位置,然后根据相邻位置的关系判断是否可以选中一个成语,选中的成语将从游戏面板中删除,并将其加入已选列表中。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值