简体   繁体   English

为什么 A 不是变量?

[英]Why isn't A a variable?

aa[i1] takes the user input and breaks it into single chars. aa[i1] 接受用户输入并将其分解为单个字符。 For example: dog becomes d, o, g.例如:dog 变成 d, o, g。 I have an if statement which if aa[i1] is equal to "A", "B", "C"... it'll tell the user so.我有一个 if 语句,如果 aa[i1] 等于“A”、“B”、“C”……它会告诉用户。 The error is when aa[i1] == A, A isn't a variable?错误是当 aa[i1] == A 时,A 不是变量?

在此处输入图片说明

First, please never post code as an image.首先,不要将代码作为图像发布。 Second, I've replaced your prompt and input with a constant for the sake of demonstration.其次,为了演示,我用常量替换了您的提示和输入。 Third, you need to surround your constant characters with single quotes (to make them constant characters, or you could define a character constant final char A = 'A'; ).第三,您需要用单引号将常量字符括起来(使它们成为常量字符,或者您可以定义一个字符常量final char A = 'A'; )。 Fourth, String has a method for copying its' internals to a char[] .第四, String有一种方法可以将其内部结构复制到char[] Finally, I would prefer printf to string concatenation.最后,我更喜欢printf而不是字符串连接。 Like,喜欢,

String a = "ANGRY AARDVARK";
char[] aa = a.toCharArray();
for (int i = 0; i < aa.length; i++) {
    System.out.printf("Character at index %d=%c%n", i, aa[i]);
    if (aa[i] == 'A') {
        System.out.println("Character is A");
    }
}

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

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