62,615
社区成员
发帖
与我相关
我的任务
分享
package com.han;
import java.io.IOException;
import java.nio.charset.Charset;
public class CSDN_Forum_1 {
public CSDN_Forum_1() {
// TODO Auto-generated constructor stub
byte[] b = new byte[1024];
System.out.println(Charset.defaultCharset().name());
try {
System.out.print("请你输入性别: ");
int length = System.in.read(b);
String input = new String(b, 0, length);
System.out.println(input);
if (input.equals("男")) {
System.out.println("先生你好");
} else if (input.equals("女")) {
System.out.println("女士你好");
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
// // the second method
// System.out.print("请你输入性别: ");
// String input = new Scanner(System.in).next();
// boolean sex = "男".equals(input);
// if (sex)
// System.out.println("先生你好");
// else
// System.out.println("女士你好");
}
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
new CSDN_Forum_1();
}
}
package com.han;
import java.io.IOException;
import java.nio.charset.Charset;
/**
* 必须在Eclipse Common属性中设置encoding为GBK。这个是用于System.in将命令行的汉字编码
* 为字节的。
* @author HAN
*
*/
public class Encoding_1 {
public Encoding_1() {
// TODO Auto-generated constructor stub
byte[] b = new byte[1024];
System.out.println(Charset.defaultCharset().name());
try {
System.out.print("请你输入性别: ");
int length = System.in.read(b);
System.out.println(length);
// 再重新从bytes到chars要使用相同的解码,另外,InputStream还包括了"\r\n"
String input = new String(b, 0, length-2, "GBK");
System.out.println(input + input.length());
if (input.equals("男")) {
System.out.println("先生你好");
} else if (input.equals("女")) {
System.out.println("女士你好");
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
new Encoding_1();
}
}
package com.han;
import java.util.Scanner;
/**
* 必须在Eclipse Common属性中设置encoding为GBK。这个是用于System.in将命令行的汉字编码
* 为字节的。
* @author HAN
*
*/
public class Encoding_2 {
public Encoding_2() {
System.out.print("请你输入性别: ");
// 再重新从bytes到chars要使用相同的解码
String input = new Scanner(System.in, "GBK").next();
boolean sex = "男".equals(input);
if (sex)
System.out.println("先生你好");
else
System.out.println("女士你好");
}
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
new Encoding_2();
}
}
package com.han;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.nio.charset.Charset;
/**
* 必须在Eclipse Common属性中设置encoding为GBK。这个是用于System.in将命令行的汉字编码
* 为字节的。
* @author HAN
*
*/
public class Encoding_3 {
public Encoding_3() {
// TODO Auto-generated constructor stub
System.out.println(Charset.defaultCharset().name());
try {
System.out.print("请你输入性别: ");
// 再重新从bytes到chars要使用相同的解码
String input = new BufferedReader(new InputStreamReader(System.in, "GBK")).readLine();
System.out.println(input + input.length());
if (input.equals("男")) {
System.out.println("先生你好");
} else if (input.equals("女")) {
System.out.println("女士你好");
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
new Encoding_3();
}
}
if (input.equals("男\r\n")) {//input 除了"男"编码2个字节外,还有"\r\n"
System.out.println("先生你好");
} else if (input.equals("女\r\n")) {
System.out.println("女士你好");//一样
}