简体   繁体   English

Java程序将大写和小写反转

[英]Java-Program to reverse upper case and lower case

I have to write a program to input a String str and change the upper case to lower case and vice versa. 我必须编写一个程序以输入String str并将大写字母更改为小写字母,反之亦然。 For example: 例如:

input: "abCD" 输入: “ abCD”

output: "ABcd" 输出: “ ABcd”

this is what I've got: 这就是我所拥有的:

l-is the length of the string l-是字符串的长度

for(int b=0;b < l;b++)

             {

               char let=str.charAt(b);

               if(let>97 && let<122)

               {char nlet=let-32;

               System.out.print(nlet);

            }
            else if(let>65 && let<90)
             { char t=let+32;
              System.out.print(t);
            }
           }
           break; 
        }

the error coming for this line:" char nlet=let-32; " is: required:char;found:int; 该行出现的错误:“ char nlet=let-32; ”是:required:char; found:int;

how do i fix this? 我该如何解决?

The issue is that 32 is an integer, and let is a char. 问题是32是一个整数,而let是一个字符。 Java will implicity convert the let value to an int when it encounters let-32 and the result is the int value (for 'a') 96 or whatever. Java遇到let-32时,它将隐式将let值转换为int,结果是int值(“ a”)为96或其他值。

You need to cast the result back to char: 您需要将结果转换回char:

(char)(let+32)

Try to use the below updated for loop: 尝试使用以下更新的for循环:

 for(int b=0;b < l;b++)

             {

               char let=str.charAt(b);

               if (Character.isLowerCase(let))
               {
                   char nlet=Character.toUpperCase(let);

               System.out.print(nlet);

              } else if(Character.isUpperCase(let))
             { char t=Character.toLowerCase(let);
              System.out.print(t);
            }
           }
           break; 
        }

Without any other classes (like Character for apparently you can't use it), you need to cast into char : 如果没有其他任何类(例如Character显然不能使用它),则需要转换为char:

for(int b=0;b < l;b++)
{
    char let=str.charAt(b);

    if(let>97 && let<122)
    {
        char nlet=(char) let-32;
        System.out.print(nlet);
    }
    else if(let>65 && let<90)
    {
        char t=(char)let+32;
        System.out.print(t);
    }
}

Your compiler told you the answer here : 您的编译器在这里告诉您答案:

required:char;found:int

It means your operation, here it is variable assignation has a wrong argument. 这意味着您的操作,这里是变量赋值有一个错误的参数。 When you have 当你有

 char nlet = xxx

The compiler expects the xxx to be castable into char . 编译器希望xxx可转换为char Here you give it a int value with let-32 or let+32 . 在这里,您可以使用let-32let+32为其提供一个int值。 So here you need to cast into char or use a function that output a char from an int ; 因此,这里需要转换为char或使用从int输出char的函数; that's where you see all the people here telling you to use Character class that gives you all helper functions to do your homework (which is also way better than manually add/sub 32) 在这里,您可以看到所有人都在告诉您使用Character类,该类为您提供了所有助手功能来完成作业(这比手动添加/替换32更好)

Careful, sometime you can see something like String str = "A string from int: " + 2000 This means your compiler will automatically cast the 2000 into "2000" because the + operation for a String takes 2 string arguments. 小心,有时您会看到类似String str = "A string from int: " + 2000这意味着编译器将自动将2000转换为"2000"因为对String+操作需要2个字符串参数。

The Problem is with your let-32; 问题出在您的let-32; it will return an integer so you need to implicitly cast it to a char variable. 它会返回一个整数,因此您需要将其隐式转换为char变量。

Change your code to char nlet=(char) (let-32); 将您的代码更改为char nlet=(char) (let-32);

Also there is one more problem in your if condition you program will not give the correct output as your are not checking for the alphabets 'a' and 'z'.Change your if -else to check for the boundary conditions. 另外,如果您没有检查字母'a'和'z',那么if条件中的另一个问题是您的程序将无法提供正确的输出。请更改if -else以检查边界条件。

if(let>=97 && let<=122)
{
}else (let>=65 && let<=90){ 
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM