你如何比较java中的字符?

2022-01-12 00:00:00 arrays char comparison java

如何比较字符(如字符串,如 variable.equals(a))?另外,我在这行代码中做错了什么:

How do you compare characters (like Strings, such as variable.equals(a))? Also, what am I doing wrong in this line of code:

    char num[] = new char[10];
    char num[0] = ' ';
    char num[1] = ' ';
    char num[2] = ' ';
    char num[3] = ' ';
    char num[4] = ' ';
    char num[5] = ' ';
    char num[6] = ' ';
    char num[7] = ' ';
    char num[8] = ' ';
    char num[9] = ' ';

我正在尝试创建一个字符数组,但它给了我一条错误消息.

I'm trying to make a array of characters, but it's giving me an error message.

推荐答案

那可能是:

char[] num = new char[10];
num[0] = ' ';
num[1] = ' ';
num[2] = ' ';
num[3] = ' ';
num[4] = ' ';
num[5] = ' ';
num[6] = ' ';
num[7] = ' ';
num[8] = ' ';
num[9] = ' ';

或者只是这样做:

char[] num = new char[] { 'K', 'E', 'V', 'I', 'N' };

要比较字符,您可以使用运算符 == 或 < 或 >

To compare character you can use the operators == or < or >

char x = 'x';
char y = 'y';

if( x <> y )
{
    //do magic
}

if( x == y )
{
    //do black magic
}

相关文章