以下这段程序接受屏幕的输入,并输出
但输入中文,显示为乱码,该如何修正!!
谢谢!!public class TestVarTypeConversion
{
public static void main(String args[])
{
int ch=0;
StringBuffer ll=new StringBuffer();
System.out.print("请输入字符:");
while(true)
{
try{ch=System.in.read();}
catch(Exception e){e.getMessage();}
if(ch=='\r'||ch=='\n')
break;
else
{ll.append((char)ch);}}
System.out.println("输入字符长度为:"+ll.length());
System.out.println("输入结果为:"+ll.toString());
}}
但输入中文,显示为乱码,该如何修正!!
谢谢!!public class TestVarTypeConversion
{
public static void main(String args[])
{
int ch=0;
StringBuffer ll=new StringBuffer();
System.out.print("请输入字符:");
while(true)
{
try{ch=System.in.read();}
catch(Exception e){e.getMessage();}
if(ch=='\r'||ch=='\n')
break;
else
{ll.append((char)ch);}}
System.out.println("输入字符长度为:"+ll.length());
System.out.println("输入结果为:"+ll.toString());
}}
{
public static void main(String args[])
{
int ch=0;
StringBuffer ll=new StringBuffer();
System.out.print("请输入字符:");
while(true)
{
try{ch=System.in.read();}
catch(Exception e){e.getMessage();}
if(ch=='\r'||ch=='\n')
break;
else
{ll.append((char)ch);}}
System.out.println("输入字符长度为:"+ll.length());
String last=new String(ll);
try{
last=new String(last.getBytes("iso_8859_1"),"gbk");
}catch(Exception e){
e.printStackTrace();
}
System.out.println("输入结果为:"+last);
}}