1

我正在尝试使用 for 循环检查一维字符串数组中的每个字符并打印“字符 [a] 为大写”或“字符 [b] 为小写”或“字符 [c] 为空格”。我还必须打印索引号和该索引中的字符。

我以为我应该用大写字母和小写字母创建单独的字符串,但结果我错了。我也不知道我是否应该将字符串数组转换为字符数组?

我想我应该使用charAt()方法,但我不知道如何。

这就是我所做的:

for(x = 0; x < fullName.length(); x++)
{
    if(fullName[0] == nameUpperCase[2]);
    {
        System.out.print("character [0] located at position [0] is  lowercase");
    }

    if(fullName[1] == nameLowerCase[4]);
    {
        System.out.print("character [b] located at position [1] is uppercase"); 
    }
}

正如你所看到的,我什至不知道如何使用循环来打印“a”或“b”,所以我不得不手动插入每一个以及它们所在的位置......

4

6 回答 6

1
fullName[0] == nameUpperCase[2]

上面的表述是错误的。我假设 fullName 是您的 for 循环中的字符串,因为您正在使用full.length()方法。fullName 不是数组,所以做 fullName[0] 是错误的。要获取字符串中的字符,请使用 String.charAt(index) 方法。作为初学者,请查看String Class API以供参考。

我想你可以做这样的事情:

    String[] s= {"Ab "};
    for(String ss: s) {
         for(int i=0; i<ss.length(); i++) {
             if(Character.isUpperCase(ss.charAt(i))) {
                 System.out.println("character "+ss.charAt(i)+" located at position"+i+ " is  lowercase");
             }
             else if(Character.isLowerCase(ss.charAt(i))) {
                 System.out.println("character "+ss.charAt(i)+" located at position"+i+ " is  lowercase");
             }
             else if(else if(Character.isSpaceChar(ss.charAt(i)))) {
                 System.out.println("character "+ss.charAt(i)+" located at position"+i+ " is  lowercase");
             }
         }
于 2012-09-29T00:57:01.313 回答
1

可能有更简单的方法,但这可能会奏效。

if (str.toUppercase().equals(str)) {
    // It's uppercase.
}

if (str.toLowerCase().equals(str)) {
    // It's lowercase.
}
于 2012-09-29T00:58:16.797 回答
1

如果您正在比较两个字符串(即数组是String[]),那么您的代码正在比较每个字符串的内存引用,这将是不同的。

为了比较String是否相等,您需要使用该equals方法。

for(x = 0; x < fullName.length(); x++)
{
    if(fullName[0].equals(nameUpperCase[2]));
    {
        System.out.print("character [0] located at position [0] is  lowercase");
    }

    if(fullName[1].equals(nameLowerCase[4]));
    {
        System.out.print("character [b] located at position [1] is uppercase"); 
    }

}

我还建议您从其他答案中汲取一些想法。

将原始转换String为 char 数组而不是...

char[] fullName = name.toCharArray();

或者您可以简单地获取给定索引处的字符...

for(x = 0; x < originalString.length(); x++)
{
    char charAt = originalString.charAt(x);
    if(Character.isUpperCase(charAt));
    {
        System.out.print("character [0] located at position [0] is  lowercase");
    }

    if(Character.toUpperCase(originalString.charAt(1)) == originalString.charAt(4));
    {
        System.out.print("character [b] located at position [1] is uppercase"); 
    }

}

更新示例

既然我们已经确定我们无法将String值与进行比较==,这里有一个我认为你想要实现的工作示例......

String value = "This is a simple string";
for (int index = 0; index < value.length(); index++) {

    String whatIs = "Unknown";
    if (Character.isUpperCase(value.charAt(index))) {
        whatIs = "Upper case";
    } else if (Character.isLowerCase(value.charAt(index))) {
        whatIs = "Lower case";
    } else if (Character.isSpaceChar(value.charAt(index))) {
        whatIs = "Space";
    } else if (Character.isDigit(value.charAt(index))) {
        whatIs = "Digit";
    }

    System.out.println("Character @ " + index + " (" + value.charAt(index) + ") is a " + whatIs + " character");

}

哪个输出

Character @ 0 (T) is a Upper case character
Character @ 1 (h) is a Lower case character
Character @ 2 (i) is a Lower case character
Character @ 3 (s) is a Lower case character
Character @ 4 ( ) is a Space character
Character @ 5 (i) is a Lower case character
Character @ 6 (s) is a Lower case character
Character @ 7 ( ) is a Space character
Character @ 8 (a) is a Lower case character
Character @ 9 ( ) is a Space character
Character @ 10 (s) is a Lower case character
Character @ 11 (i) is a Lower case character
Character @ 12 (m) is a Lower case character
Character @ 13 (p) is a Lower case character
Character @ 14 (l) is a Lower case character
Character @ 15 (e) is a Lower case character
Character @ 16 ( ) is a Space character
Character @ 17 (s) is a Lower case character
Character @ 18 (t) is a Lower case character
Character @ 19 (r) is a Lower case character
Character @ 20 (i) is a Lower case character
Character @ 21 (n) is a Lower case character
Character @ 22 (g) is a Lower case character
于 2012-09-29T01:03:32.777 回答
1

如果您使用的是String,您可以简单地使用charAt来选择字符。不可能使用例如 . 来选择单个字符fullName[0]。如果是字符数组,则此表示法是可能的。fullName

String fullName = "Stack Overflow";
for (int x = 0; x < fullName.length(); x++)
{
   char testChar = fullName.charAt(x);
   if (Character.isLowerCase(testChar))
   {
     System.out.println("character [" + testChar + "] located at position [" + x + "] is lowercase");
   }
   else if (Character.isUpperCase(testChar))
   {
      System.out.println("character [" + testChar + "] located at position [" + x + "] is uppercase");
   }
   else if (Character.isSpaceChar(testChar)) {
     System.out.println("character [" + testChar + "] located at position [" + x + "] is a space");
  }
}
于 2012-09-29T01:05:00.613 回答
0

你可能想试试,

    for(x = 0; x < fullName.length(); x++)
    {
        if( fullName[x].equals(nameUpperCase[x]) );
        {
            System.out.print("character [" + fullName[x] + "] located at position [" + x +       "] is lowercase.");
        }

        if( fullName[x].equals(nameLowerCase[x]) );
        {
            System.out.print("character [" + fullName[x] + "] located at position [" + x + "] is  uppercase."); 
        }

    }

如果fullName、nameUpperCase 和 nameLowerCase是字符串数组,它应该可以正常工作。虽然,查看其他人发布的解决方案,我建议您查看那些更清晰和更清洁的代码。

于 2012-09-29T00:56:32.287 回答
0

你可能想要这样的东西:

String s = "Hello World";
for (int i = 0 ; i < s.length() ; i++) {
    char c = s.charAt(i);
    if (c == ' ')
        System.out.println("character located at position [" + i + "] is a space");
    else if (Character.isUpperCase(c))
        System.out.println("character [" + c + "] located at position [" + i + "] is uppercase");
    else if (Character.isLowerCase(c))
        System.out.println("character [" + c + "] located at position [" + i + "] is lowercase");
    // else character is not recognized
}

当然,我在String这里只使用了一个,但在 a 的情况下String[],您可以简单地将这个过程应用于每个成员。

于 2012-09-29T01:04:00.330 回答