我正在尝试创建一个简短的程序,它将所有大写字母转换为小写字母(从命令行输入).
I'm trying to create a short program that would convert all letters that are uppercase to lowercase (from the command line input).
以下编译但没有给我我期望的结果.这会是什么原因??
The following compiles but does not give me the result I am expecting. What would be the reason for this??
例如)java toLowerCase BANaNa -> 给出香蕉的输出
Eg) java toLowerCase BANaNa -> to give an output of banana
public class toLowerCase{
public static void main(String[] args){
toLowerCase(args[0]);
}
public static void toLowerCase(String a){
for (int i = 0; i< a.length(); i++){
char aChar = a.charAt(i);
if (65 <= aChar && aChar<=90){
aChar = (char)( (aChar + 32) );
}
System.out.print(a);
}
}
}
对我来说看起来像是作业,只是一个提示.您正在打印字符串 a,而您正在修改 char 类型 aChar,而不是修改原始字符串 a.(记住字符串是不可变的).
Looks like homework to me, Just a hint. You are printing string a whereas you are modifying the char type aChar, its not modifying the original string a. (Remember strings are immutable).
这篇关于Java - 将小写转换为大写而不使用 toUppercase()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!
“Char 不能被取消引用"错误quot;Char cannot be dereferencedquot; error(“Char 不能被取消引用错误)
Java Switch 语句 - 是“或"/“和"可能的?Java Switch Statement - Is quot;orquot;/quot;andquot; possible?(Java Switch 语句 - 是“或/“和可能的?)
Java替换字符串特定位置的字符?Java Replace Character At Specific Position Of String?(Java替换字符串特定位置的字符?)
具有 int 和 char 操作数的三元表达式的类型是什么What is the type of a ternary expression with int and char operands?(具有 int 和 char 操作数的三元表达式的类型是什么?)
读取文本文件并存储出现的每个字符Read a text file and store every single character occurrence(读取文本文件并存储出现的每个字符)
为什么我需要在 byte 和 short 上显式转换 char 原语Why do I need to explicitly cast char primitives on byte and short?(为什么我需要在 byte 和 short 上显式转换 char 原语?)