可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
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??
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);
}
}
}
回答1:
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).
回答2:
You are printing the String
a
, without modifying it. You can print char directly in the loop as follows:
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(aChar);
}
}
}
回答3:
A cleaner way of writing this code is
public static void printLowerCase(String a){
for(char ch: a.toCharArray()) {
if(ch >= 'A' && ch <= 'Z')
ch += 'a' - 'A';
System.out.print(ch);
}
}
Note: this will not work for upper case characters in any other range. (There are 1,000s of them)
回答4:
Looks like you're close. :)
For starters...
char aChar = a.charAt(i);
"a" is an array of Strings, so I believe you would want to iterate over each element
char aChar = a[i].charAt(0);
and it also seems like you want to return the value of the modified variable, not of "a" which was the originally passed in variable.
System.out.print(aChar);
not
System.out.print(a);
Hope that helps you.
回答5:
public static void toLowerCase(String a){
String newStr = "";
for (int i = 0; i< a.length(); i++){
char aChar = a.charAt(i);
if (65 <= aChar && aChar<=90){
aChar = (char)( (aChar + 32) );
}
newStr = newStr + aChar;
}
System.out.println(newStr);
}
You should print newStr
outside for loop. You were trying to print it inside the loop
回答6:
/**
* Method will convert the Lowercase to uppercase
* if input is null, null will be returned
* @param input
* @return
*/
public static String toUpperCase(String input){
if(input == null){
return input;
}
StringBuilder builder = new StringBuilder();
for(int i=0;i<input.length();i++){
char stringChar = input.charAt(i);
if(92 <= stringChar && stringChar <=122){
stringChar = (char)( (stringChar - 32) );
builder.append(stringChar);
}
else if (65 <= stringChar && stringChar<=90)
{
builder.append(stringChar);
}
}
if(builder.length() ==0){
builder.append(input);
}
return builder.toString();
}
回答7:
public class Changecase
{
static int i;
static void changecase(String s)
{
for(i=0;i<s.length();i++)
{
int ch=s.charAt(i);
if(ch>64&&ch<91)
{
ch=ch+32;
System.out.print( (char) ch);
}
else if(ch>96&&ch<123)
{
ch=ch-32;
System.out.print( (char) ch);
}
if(ch==32)
System.out.print(" ");
}
}
public static void main (String args[])
{
System.out.println("Original String is : ");
System.out.println("Alive is awesome ");
Changecase.changecase("Alive is awesome ");
}
}
回答8:
public class MyClass
{
private String txt;
private char lower;
public MyClass(String txt)
{
this.txt = txt;
}
public void print()
{
for(int i=0;i<txt.length();i++)
{
if('A' <= txt.charAt(i) && txt.charAt(i) <= 'Z')
{
lower = (char)(txt.charAt(i) + 32);
System.out.print(lower);
}
else
{
lower = txt.charAt(i);
System.out.print(lower);
}
}
}
public static void main(String[] args)
{
MyClass mc = new MyClass("BaNaNa");
mc.print();
}
}
Sorry pretty late to the scene but this should solve it. An else condition because when it is not zero it totally discards the alphabet.
回答9:
If somebody needs clear code without MagicNumbers and as less as possible conversions here is my solution:
final char[] charArray = new char[string.length()];
for (int i = 0; i < string.length(); i++) {
char c = string.charAt(i);
charArray[i] = Character.isLowerCase(c) ? Character.toUpperCase(c) : Character.toLowerCase(c);
}
String.valueOf(charArray);
回答10:
import java.util.Scanner;
public class LowerToUpperC {
public static void main(String[] args) {
char ch;
int temp;
Scanner scan = new Scanner(System.in);
System.out.print("Enter a Character in Lowercase : ");
ch = scan.next().charAt(0);
temp = (int) ch;
temp = temp - 32;
ch = (char) temp;
System.out.print("Equivalent Character in Uppercase = " +ch);
}
}