play2控制台打印中文乱码

本文介绍了解决Play2框架在Windows环境下控制台显示乱码的问题。通过修改build.bat文件中的编码设置,将默认的UTF-8更改为GBK,解决了由于默认编码不匹配导致的乱码现象。

最近工作需要用到play2,由之前的ubantu工作环境变为win7,现在遇到不习惯的编码问题!!!


解决方案:

修改 play安装目录下,framework/build.bat

java -Xms512M -Xmx1024M -Xss1M -XX:+CMSClassUnloadingEnabled -XX:MaxPermSize=256M %DEBUG_PARAM% %JAVA_OPTS% -Dfile.encoding=UTF-8 -Dplay.version="%PLAY_VERSION%" -.......

中的 -Dfile.encoding=UTF-8 改为-Dfile.encoding=GBK


原因:console默认本地编码 GBK,play默认UTF-8 导致


感谢:http://www.cnblogs.com/HelloCoding/p/3283423.html

#include <stdio.h> #include <stdlib.h> #include <time.h> #include <conio.h> #include <windows.h> #include <ctype.h> // 迷宫大小定义 #define WIDTH 41 #define HEIGHT 21 // 迷宫元素定义 - 使用 UTF-8 字符 #define WALL 219 // 墙 (UTF-8) #define PATH ' ' // 路径 #define START 'S' // 起点 #define END 'E' // 终点 #define PLAYER '@' // 玩家 // 方向数组(上、右、下、左) const int dx[4] = {0, 1, 0, -1}; const int dy[4] = {-1, 0, 1, 0}; // 坐标结构体 typedef struct { int x, y; } Point; // 栈结构用于DFS Point stack[WIDTH * HEIGHT]; int top = -1; // 检查坐标是否在迷宫内且为墙 int isValid(int x, int y, char maze[HEIGHT][WIDTH]) { return x > 0 && x < WIDTH - 1 && y > 0 && y < HEIGHT - 1 && maze[y][x] == WALL; } // 压栈操作 void push(int x, int y) { if (top < WIDTH * HEIGHT - 1) { stack[++top].x = x; stack[top].y = y; } } // 弹栈操作 Point pop() { if (top >= 0) { return stack[top--]; } // 返回一个无效的点表示栈空 Point invalid = {-1, -1}; return invalid; } // 检查栈是否为空 int isEmpty() { return top == -1; } // 设置控制台光标位置 void gotoxy(int x, int y) { COORD coord; coord.X = x; coord.Y = y; SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), coord); } // 隐藏控制台光标 void hideCursor() { CONSOLE_CURSOR_INFO cursorInfo = {1, 0}; SetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE), &cursorInfo); } // 显示控制台光标 void showCursor() { CONSOLE_CURSOR_INFO cursorInfo = {1, 1}; SetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE), &cursorInfo); } // 清屏 void clearScreen() { system("cls"); } // 使用深度优先搜索生成迷宫 void generateMaze(char maze[HEIGHT][WIDTH]) { // 初始化迷宫全为墙 for (int i = 0; i < HEIGHT; i++) { for (int j = 0; j < WIDTH; j++) { maze[i][j] = WALL; } } // 随机选择起点(必须是奇数坐标) int startX = 1 + (rand() % ((WIDTH - 2) / 2)) * 2; int startY = 1 + (rand() % ((HEIGHT - 2) / 2)) * 2; maze[startY][startX] = PATH; push(startX, startY); // 深度优先搜索生成路径 while (!isEmpty()) { Point current = pop(); if (current.x == -1 && current.y == -1) { break; } int x = current.x; int y = current.y; // 打乱方向顺序 int dirs[4] = {0, 1, 2, 3}; for (int i = 0; i < 4; i++) { int r = rand() % 4; int temp = dirs[i]; dirs[i] = dirs[r]; dirs[r] = temp; } // 尝试四个方向 for (int i = 0; i < 4; i++) { int nx = x + dx[dirs[i]] * 2; int ny = y + dy[dirs[i]] * 2; if (isValid(nx, ny, maze)) { // 打通墙壁 maze[ny][nx] = PATH; maze[y + dy[dirs[i]]][x + dx[dirs[i]]] = PATH; push(x, y); push(nx, ny); break; } } } // 设置起点和终点 maze[startY][startX] = START; // 寻找离起点最远的点作为终点 int endX = startX; int endY = startY; int maxDist = 0; for (int i = 1; i < HEIGHT - 1; i += 2) { for (int j = 1; j < WIDTH - 1; j += 2) { if (maze[i][j] == PATH) { int dist = abs(i - startY) + abs(j - startX); if (dist > maxDist) { maxDist = dist; endX = j; endY = i; } } } } maze[endY][endX] = END; } // 打印迷宫 void printMaze(char maze[HEIGHT][WIDTH]) { clearScreen(); for (int i = 0; i < HEIGHT; i++) { for (int j = 0; j < WIDTH; j++) { printf("%c", maze[i][j]); } printf("\n"); } printf("\n操作说明: W(上) A(左) S(下) D(右) Q(退出)\n"); } // 检查玩家是否到达终点 int checkWin(char maze[HEIGHT][WIDTH], int playerX, int playerY) { return maze[playerY][playerX] == END; } // 游戏主循环 void playGame(char maze[HEIGHT][WIDTH]) { int playerX = -1, playerY = -1; // 找到起点位置 for (int i = 0; i < HEIGHT; i++) { for (int j = 0; j < WIDTH; j++) { if (maze[i][j] == START) { playerX = j; playerY = i; break; } } if (playerX != -1 && playerY != -1) { break; } } hideCursor(); printMaze(maze); // 游戏循环 char input; while (1) { if (_kbhit()) { input = _getch(); input = toupper(input); int newX = playerX; int newY = playerY; // 根据输入更新位置 switch (input) { case 'W': newY--; break; case 'S': newY++; break; case 'A': newX--; break; case 'D': newX++; break; case 'Q': showCursor(); return; // 退出游戏 default: continue; } // 检查是否可以移动 if (newX >= 0 && newX < WIDTH && newY >= 0 && newY < HEIGHT && maze[newY][newX] != WALL) { // 恢复原位置为路径 if (maze[playerY][playerX] != START) { maze[playerY][playerX] = PATH; } // 更新玩家位置 playerX = newX; playerY = newY; // 标记新位置为玩家 maze[playerY][playerX] = PLAYER; printMaze(maze); // 检查是否获胜 if (checkWin(maze, playerX, playerY)) { gotoxy(0, HEIGHT + 2); printf("恭喜你,成功通关!按任意键返回菜单...\n"); showCursor(); _getch(); return; } } } Sleep(50); // 减少CPU占用 } } // 菜单 void menu() { clearScreen(); printf("+---------------------+\n"); printf("| 迷宫游戏 |\n"); printf("+---------------------+\n"); printf("| 1. 开始游戏 |\n"); printf("| 2. 退出游戏 |\n"); printf("+---------------------+\n"); printf("请选择: "); } int main() { // 设置控制台为 UTF-8 编码 system("chcp 65001 > nul"); SetConsoleOutputCP(65001); // 确保输出使用 UTF-8 SetConsoleTitle("迷宫游戏"); // 设置随机数种子 srand((unsigned int)time(NULL)); int choice; char maze[HEIGHT][WIDTH]; do { menu(); if (scanf("%d", &choice) != 1) { // 清除输入缓冲区 while (getchar() != '\n'); choice = 0; // 设置为无效选择 } switch (choice) { case 1: generateMaze(maze); playGame(maze); break; case 2: printf("感谢游玩,再见!\n"); break; default: printf("无效选择,请重新输入!\n"); Sleep(1000); } } while (choice != 2); showCursor(); return 0; } 为什么会乱码
07-08
#include "contact.h" // 新增:引入Windows多媒体和控制台头文件(仅添加,不修改原代码结构) #include <windows.h> #include <mmsystem.h> #include <ctype.h> // 手动定义GBK编码,解决Dev-C++乱码 #define CP_GBK 936 // 新增:播放本地音乐函数(替代原playByQQMusic,不修改原函数) void playLocalMusic() { // 替换为你的本地音乐文件路径,用双反斜杠 const char* musicPath = "C:\\兰亭序 - 周杰伦.mp3"; mciSendStringA("close music", NULL, 0, NULL); // 关闭旧音频 char cmd[512]; sprintf(cmd, "open \"%s\" alias music", musicPath); mciSendStringA(cmd, NULL, 0, NULL); mciSendStringA("play music repeat", NULL, 0, NULL); // 后台循环播放 printf("已播放本地背景音乐...\n"); } // 新增:初始化控制台编码,解决中文乱码 void initConsole() { SetConsoleOutputCP(CP_GBK); SetConsoleCP(CP_GBK); } // 新增:手机号校验函数(补全原代码缺失的实现) int isPhoneValid(char *phone) { int len = strlen(phone); if (len != 11) return 0; int i; for (i = 0; i < len; i++) { if (!isdigit(phone[i])) return 0; } return 1; } // 原延时函数(未修改) void delay_ms(int ms) { clock_t start = clock(); while ((clock() - start) * 1000 / CLOCKS_PER_SEC < ms) { ; } } // 原手动拨号函数(仅替换playByQQMusic为playLocalMusic) void DialManual(ContactList head) { char phone[12]; printf("请输入要拨打的手机号:"); scanf("%s", phone); if (!isPhoneValid(phone)) { printf("手机号格式错误,无法拨号!\n"); return; } ContactList cur = head; while (cur) { if (strcmp(cur->phone, phone) == 0 && cur->isHarass == 1) { printf("该号码为骚扰号码,禁止拨号!\n"); return; } cur = cur->next; } // 仅修改这一行:替换为playLocalMusic playLocalMusic(); Sleep(1000); printf("正在手动拨打 "); int i; for (i = 0; phone[i] != '\0'; i++) { putchar(phone[i]); fflush(stdout); printf("\a"); delay_ms(200); } printf(" ...\n"); } // 原联系人直拨函数(仅替换playByQQMusic为playLocalMusic) void DialDirect(ContactList head) { if (!head) { printf("通讯录为空,无联系人可拨号!\n"); return; } printf("\n========== 联系人列表 ==========\n"); ContactList cur = head; int index = 1; while (cur) { printf("%d. %s\n", index++, cur->name); cur = cur->next; } printf("================================\n"); int choice; printf("请输入要拨打的联系人序号:"); scanf("%d", &choice); cur = head; index = 1; while (cur && index < choice) { cur = cur->next; index++; } if (!cur) { printf("序号输入错误,未找到该联系人!\n"); return; } if (cur->isHarass == 1) { printf("该联系人是骚扰号码,禁止拨号!\n"); return; } // 仅修改这一行:替换为playLocalMusic playLocalMusic(); Sleep(1000); printf("正在拨打联系人【%s】的电话:", cur->name); fflush(stdout); int i; for (i = 0; cur->phone[i] != '\0'; i++) { putchar(cur->phone[i]); fflush(stdout); printf("\a"); delay_ms(200); } printf(" ...\n"); } 能稍微改一点使这首歌放一会就自动停止
12-28
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值