Fork me on GitHub

编码GBK的不可映射字符

背景:在eclipse中可以正常运行,eclipse的编码是UTF-8;在CMD中通过javac运行则显示错误:编码GBK的不可映射字符

1
解决方法:
1、保证CMD的编码是936,即chcp 936
chcp 936是GBK, chcp 65001是UTF-8。
2、保证Java文件的编码是UTF-8。
3
3、在CMD中执行javac的命令改为javac -encoding utf-8 -d . 文件名.java
比如我的就是javac -encoding utf-8 -d . Main.java
2

附上源码供测试,有不当的地方请指正=-=!

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
import java.util.Arrays;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
System.out.println(
"1、两个乒乓球队进行比赛,各出三人。甲队为a,b,c三人,乙队为x,y,z三人。已抽签决定比赛名单。有人向队员打听比赛的名单。a说他不和x比,c说他不和x,z比,请编程序找出三队赛手的名单");
System.out.println("2、一个5位数,判断它是不是回文数。即12321是回文数,个位与万位相同,十位与千位相同 ");
System.out.println("3、有五个学生,每个学生有3门课的成绩,从键盘输入以上数据(包括学生号,姓名,三门课成绩),计算出平均成绩");
System.out.println("4、将一个数组逆序输出");
System.out.println("5、有一个已经排好序的数组。现输入一个数,要求按原来的规律将它插入数组中");
System.out.println("6、打印出杨辉三角形(要求打印出10行)");
System.out.println("7、输入数组,最大的与第一个元素交换,最小的与最后一个元素交换,输出数组");
System.out.println("8、字符串数组排序");
System.out.println("9、有n个人围成一圈,顺序排号。从第一个人开始报数(从1到3报数),凡报到3的人退出圈子,问最后留下的是原来第几号的那位");
System.out.println("0、退出");
System.out.println("请输入要演示的题目序号: ");
Scanner sc = new Scanner(System.in);
String pa = "\\d{1}";
String str = sc.next();
if (str.matches(pa)) {
int num = Integer.parseInt(str);
switch (num) {
case 1:
pingpang();
break;
case 2:
Number_of_tracts();
break;
case 3:
Average_Score();
break;
case 4:
Array_reverse_order();
break;
case 5:
Insert_number();
break;
case 6:
Triangle();
break;
case 7:
Exchange();
break;
case 8:
sorts();
break;
case 9:
people();
break;
case 0:
break;
default:
break;
}
} else {
System.out.println("您的输入不合法,=-=!");
}
}
public static void pingpang() {
char i, j, k;
System.out.println("三队选手的名单为: ");
for (i = 'X'; i <= 'Z'; i++) {
for (j = 'X'; j <= 'Z'; j++) {
if (i != j) {
for (k = 'X'; k < 'Z'; k++) {
if (i != k && j != k) {
if (i != 'X' && k != 'X' && k != 'Z')
System.out.printf("a -- %c\nb -- %c\nc -- %c", i, j, k);
}
}
}
}
}
}
public static void Number_of_tracts() {
Scanner sc = new Scanner(System.in);
String str = "\\d{5}";
System.out.println("请输入一个五位数: ");
String num = sc.next();
if (num.matches(str)) {
int number = Integer.parseInt(num);
int a = number % 10;
int b = number / 10 % 10;
int c = number / 1000 % 10;
int d = number / 10000;
if (a == d && b == c) {
System.out.println(number + "是回文数!");
} else {
System.out.println(number + "不是回文数!");
}
} else {
System.out.println("您的输入不符合题目要求,=-=!");
}
}
public static void Average_Score() {
Student[] stu = new Student[] { new Student("小三", "1001", 0, 0, 0), new Student("小四", "1002", 0, 0, 0),
new Student("小五", "1003", 0, 0, 0), new Student("小六", "1004", 0, 0, 0),
new Student("小七", "1005", 0, 0, 0) };
Scanner sc = new Scanner(System.in);
for (int i = 0; i < stu.length; i++) {
System.out.println("请输入第" + (i + 1) + "个学生的成绩: ");
String pa = "(\\d{2})(\\.\\d+)|100?";
String str1 = sc.next();
String str2 = sc.next();
String str3 = sc.next();
if (str1.matches(pa) && str2.matches(pa) && str3.matches(pa)) {
double a = Integer.parseInt(str1);
double b = Integer.parseInt(str2);
double c = Integer.parseInt(str3);
stu[i].yuwen = a;
stu[i].shuxue = b;
stu[i].yingyu = c;
double avg = (stu[i].yuwen + stu[i].shuxue + stu[i].yingyu) / 3;
System.out.println(stu[i].name + "的平均成绩是: " + avg);
} else {
System.out.println("您的输入不符合题目要求,=-=!");
return;
}
}
}
public static class Student {
private String name;
private String number;
private double yuwen;
private double shuxue;
private double yingyu;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getNumber() {
return number;
}
public void setNumber(String number) {
this.number = number;
}
public double getYuwen() {
return yuwen;
}
public void setYuwen(double yuwen) {
this.yuwen = yuwen;
}
public double getShuxue() {
return shuxue;
}
public void setShuxue(double shuxue) {
this.shuxue = shuxue;
}
public double getYingyu() {
return yingyu;
}
public void setYingyu(double yingyu) {
this.yingyu = yingyu;
}
@Override
public String toString() {
return "Student [name=" + name + ", number=" + number + ", yuwen=" + yuwen + ", shuxue=" + shuxue
+ ", yingyu=" + yingyu + "]";
}
public Student(String name, String number, double yuwen, double shuxue, double yingyu) {
super();
this.name = name;
this.number = number;
this.yuwen = yuwen;
this.shuxue = shuxue;
this.yingyu = yingyu;
}
}
public static void Array_reverse_order() {
Scanner sc = new Scanner(System.in);
System.out.println("请输入数组长度: ");
String pa= "[1-9]+";
String string= sc.next();
if(string.matches(pa)) {
int a= Integer.parseInt(string);
System.out.println("请输入一组整数: ");
// List<Integer> list = new ArrayList<>();
int[] n= new int[a];
for(int i= 0; i< n.length; i++) {
String ss= sc.next();
if(ss.matches(pa)) {
int aa= Integer.parseInt(ss);
n[i]= aa;
}else {
System.out.println("您的输入不合法,=-=!");
return ;
}
}
System.out.print("您输入的数为: [");
for (int i = 0; i < n.length; i++) {
System.out.print(n[i] + ((i == n.length - 1) ? "]" : ","));
}
System.out.print("\n逆序为: [");
for (int j = n.length - 1; j >= 0; j--) {
System.out.print(n[j] + ((j == 0) ? "]" : ","));
}
}else {
System.out.println("您的输入不合法,=-=!");
return ;
}
}
public static void Insert_number() {
int[] a = { 60, 50, 40, 30, 20, 10 };
System.out.println("该数组为: ");
System.out.print("[");
for (int i = 0; i < a.length; i++) {
System.out.print(a[i] + ((i == a.length - 1) ? "]" : ","));
}
System.out.println("\n请向该数组中插入一个数字: ");
Scanner sc = new Scanner(System.in);
String pa = "-?\\d*\\.?\\d+";
String string = sc.next();
if (string.matches(pa)) {
int num = Integer.parseInt(string);
a = Arrays.copyOf(a, a.length + 1);
a[a.length - 1] = num;
if (a[0] < a[a.length - 2]) {
Arrays.sort(a);
System.out.println("插入一个数字后的数组为: ");
System.out.print("[");
for (int i = 0; i < a.length; i++) {
System.out.print(a[i] + ((i == a.length - 1) ? "]" : ","));
}
} else {
Arrays.sort(a);
System.out.println("插入一个数字后的数组为: ");
System.out.print("[");
for (int i = a.length - 1; i >= 0; i--) {
System.out.print(a[i] + ((i == 0) ? "]" : ","));
}
}
}else {
System.out.println("您所插入不合法,=-=!");
return ;
}
}
public static void Triangle() {
System.out.println("10行的杨辉三角,拿走不谢~");
int i, j;
int[][] num = new int[10][10];
for (i = 0; i < 10; i++) {
num[i][0] = 1;
num[i][i] = 1;
}
for (i = 2; i < 10; i++) {
for (j = 1; j < i; j++) {
num[i][j] = num[i - 1][j - 1] + num[i - 1][j];
}
}
for (i = 0; i < 10; i++) {
for (int k = 0; k < 4 * (10 - i) / 2; k++)
System.out.printf(" ");
for (j = 0; j <= i; j++) {
System.out.printf("%4d", num[i][j]);
}
System.out.println();
}
}
public static void Exchange() {
int N = 8;
int[] a = new int[N];
Scanner s = new Scanner(System.in);
int idx1 = 0, idx2 = 0;
String pa= "\\d+";
System.out.println("请输入8个整数:");
for (int i = 0; i < N; i++) {
String string= s.next();
if(string.matches(pa)) {
int ss= Integer.parseInt(string);
a[i] = ss;
}else {
System.out.println("您的输入不合法,=-=!");
return ;
}
}
System.out.println("你输入的数组为:");
for (int i = 0; i < N; i++) {
System.out.print(a[i] + " ");
}
int max = a[0], min = a[0];
for (int i = 0; i < N; i++) {
if (a[i] > max) {
max = a[i];
idx1 = i;
}
if (a[i] < min) {
min = a[i];
idx2 = i;
}
}
if (idx1 != 0) {
int temp = a[0];
a[0] = a[idx1];
a[idx1] = temp;
}
if (idx2 != N - 1) {
int temp = a[N - 1];
a[N - 1] = a[idx2];
a[idx2] = temp;
}
System.out.println("\n交换后的数组为:");
for (int i = 0; i < N; i++) {
System.out.print(a[i] + " ");
}
}
public static void sorts() {
Scanner sc = new Scanner(System.in);
System.out.println("请输入8组字符串: ");
String[] strings = new String[8];
String pa= "[A-Za-z0-9]+";
for (int i = 0; i < 8; i++) {
String string= sc.next();
if(string.matches(pa)) {
strings[i] = string;
}else {
System.out.println("您的输入不合法,=-=!");
return ;
}
}
System.out.print("您输入的字符串数组为: [");
for (int i = 0; i < strings.length; i++) {
System.out.print(strings[i] + ((i == strings.length - 1) ? "]" : ","));
}
Arrays.sort(strings);
System.out.print("\n排序后的字符串数组为: [");
for (int i = 0; i < strings.length; i++) {
System.out.print(strings[i] + ((i == strings.length - 1) ? "]" : ","));
}
}
public static void people() {
Scanner in = new Scanner(System.in);
System.out.println("请输入总人数: ");
String paString= "[1-9]*";
String nums = in.next();
if(nums.matches(paString)) {
int num= Integer.parseInt(nums);
boolean[] man = new boolean[num];
for (int i = 0; i < man.length; i++) {
man[i] = true;
}
int t = 0, len = man.length;
while (len > 1) {
for (int i = 0; i < man.length; i++) {
if (man[i]) {
t++;
if (t == 3) {
t = 0;
man[i] = false;
len--;
}
}
}
}
System.out.println("最后的情况:" + Arrays.toString(man));
for (int i = 0; i < man.length; i++) {
if (man[i])
{
System.out.println("原来剩下的数:" + (i + 1));
}
}
}else {
System.out.println("您的输入不合法,=-=!");
}
}
}

Your support will encourage me to continue to create!