java 如何解决从 int 到 char 的错误有损转换?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/44257827/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to solve error lossy converion from int to char?
提问by user8082934
I am begginer.While compiling this code
我是 begginer.While 编译这段代码
class UpperLowerCase
{
static String ini = "I LOVE JAVA";
static char str[] = ini.toCharArray();
static char invr[] = new char[10];
static int len = 0 , len1 = 0, i;
static void toUpper()
{
for(i=0;i<str.length;i++)
{
if(str[i]>=97 && str[i]<=122)
invr[len1++]=(str[i]-32);
else
invr[len++]=str[i];
}
invr[len1]='invr[len1++] = str[i]-32;
';
System.out.println("Reverse of"+ str+" is \n"+ invr);
}
static void toLower()
{
for(i=0;i<str.length;i++)
{
if(str[i]>=65 && str[i]<=90)
invr[len1++]=str[i]-32;
else
invr[len++]=str[i];
}
invr[len1]='invr[len1++] = (char)(str[i]-32);
';
System.out.println("Reverse of"+ str+" is \n"+ invr);
}
public static void main(String [] args)
{
toUpper();
}
}
I get error "possible lossy conversion from int to char"
我收到错误“从 int 到 char 的可能有损转换”
How to convert int to char.
如何将 int 转换为 char。
Please help me by telling how to resolve this error?
请帮助我告诉如何解决此错误?
回答by ΦXoc? ? Пepeúpa ツ
You can have an overflow when doing mathematic opetations between integers and chars, this can affect the expected result since that value is going to be assigned to a char... here you have
在整数和字符之间进行数学运算时可能会溢出,这可能会影响预期结果,因为该值将被分配给一个字符......在这里你有
##代码##but you should cast to char like:
但你应该像这样转换为 char:
##代码##