一多半是编码问题,但是找不出来修改方案:
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();
}}
求解释~~~

解决方案 »

  1.   


                if (input.equals("男\r\n")) {//input 除了"男"编码2个字节外,还有"\r\n"
                    System.out.println("先生你好");
                } else if (input.equals("女\r\n")) {
                    System.out.println("女士你好");//一样
                }
      

  2.   

    解决了,但是要在Eclipse的Run Configuration的Common中修改Encoding为GBK(因为默认是UTF-8)。我请问下能不能在程序中强行指定?我试了下好像不能用System.setProperty("file.encoding","GBK"); 
      

  3.   


    这样就行了:
    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("男\r\n")) {
                    System.out.println("先生你好");
                } else if (input.equals("女\r\t")) {
                    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();
        }
     
    }
      

  4.   

    这个我用楼上几位不同的方法早已解决了,但是要在Eclipse的Run Configuration的Common中修改Encoding为GBK(因为默认是UTF-8)。我请问下能不能在程序中强行指定?我试了下好像不能用System.setProperty("file.encoding","GBK"); 附上3种不同的代码:
    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();
    }}
      

  5.   

    用Scanner读命令行输入也要在Eclipse的Run Configuration的Common中修改Encoding为GBK(因为默认是UTF-8)。否则不行。我请问下能不能在程序中强行指定?我试了下好像不能用System.setProperty("file.encoding","GBK"); 
      

  6.   

    再顶一下: 
    在Eclipse的Run Configuration的Common中修改Encoding为GBK(因为默认是UTF-8)。好像是设置的defaultCharset,可以通过System.out.println(Charset.defaultCharset().name());来查询。我们怎么通过程序代码来实现设置defaultCharset呢?
      

  7.   

    额 你学到什么知识点啊。。我的问题还没有解决啊。为什么可以在Eclipse的Run Configuration的Common中修改Encoding为GBK(因为默认是UTF-8)。好像是设置的defaultCharset,可以通过System.out.println(Charset.defaultCharset().name());来查询。我们怎么通过程序代码来实现设置defaultCharset呢?或者说如果脱离了Eclipse,又怎么设置defaultCharset以保证程序的正常运行呢?
      

  8.   

    哎 看来大家还是没明白我的问题啊,我已经用3种方法实现了(上面的帖子代码),包括Scanner方法。但是无论哪种方法都要设置defaultCharset为GBK,设置方法为在Eclipse的Run Configuration的Common中修改Encoding为GBK(因为我的默认是UTF-8,而UTF-8程序不能正常运行,即得不到“先生你好”)。我现在想知道除了这种方法修改还可以通过其他方法吗,比如没有Eclipse我们该怎样?
      

  9.   

          引用 6 楼 huntor 的回复:改成使用Scanner读命令行输入           
            +1