简体   繁体   English

在for循环中添加到ASCII代码

[英]Adding to ascii code during for loop

This is probably a simple fix but I can't seem to solve it. 这可能是一个简单的修复程序,但我似乎无法解决。

I am trying to add an integer to the ascii value of characters during a for loop. 我试图在for循环中向字符的ascii值添加一个整数。

It is giving me the error that the program expects a variable rather than a value. 这给了我一个错误,该程序期望变量而不是值。 How can I do what I am trying to do here? 我该怎么办?

Here is the code: 这是代码:

public boolean toggleEncryption(){
    if(encrypted == false){
        for(int i = 0; i < sentence.length(); i++){
            if(sentence.charAt(i) >= 65 && sentence.charAt(i) <= 90){
                int x = (int)sentence.charAt(i);
                x += key;
                while(x > 90){
                    x = x - 26;
                }
                sentence.charAt(i) += (char)x;
            }
        }
    }
    return encrypted;
}

the line sentence.charAt(i) += (char)x; sentence.charAt(i) += (char)x; is not working for me 对我不起作用

Simple: 简单:

sentence.charAt(i) += (char)x;

You wrongly assume that charAt() gives you a "left hand side" thingy. 您错误地认为charAt()给您“左侧”的麻烦。 In other words: something that you can assign a value to; 换句话说:您可以为其分配值的内容; like a variable. 像一个变量。

But this is not possible: charAt() returns an char value; 但这是不可能的: charAt()返回一个char值; that represents the char within the string at that index. 代表该索引处字符串中的char。

It does not give you something that allows you to manipulate the string itself! 没有给您提供让您操纵字符串本身的东西! Strings are immutable; 字符串是不可变的。 you can't use charAt() to modify its content! 您不能使用charAt()修改其内容!

In other words; 换一种说法; you can do this: 你可以这样做:

char c = 'a';
c += 'b';

but you can't use charAt() to achieve the same! 但是您不能使用charAt()实现相同的效果!

Thus, in order to make your code work, you have to build a new string, like: 因此,为了使代码正常工作,您必须构建一个新的字符串,例如:

StringBuilder builder = new StringBuilder(sentence.length());
for(int i = 0; i < sentence.length(); i++) {
  if(sentence.charAt(i) >= 65 && sentence.charAt(i) <= 90){
    int x = (int)sentence.charAt(i);
    x += key;
    while(x > 90){
      x = x - 26;
    }
    builder.append(sentence.charAt(i) + (char)x));
  } else {
    builder.append(sentence.charAt(i)); 
  }
}

(disclaimer: I just wrote down the above code; there might be typos or little bugs in there; it is meant to be "pseudo code" to get you going!) (免责声明:我只是写下了上面的代码;其中可能有错别字或小错误;它的意思是“伪代码”才能使您前进!)

Beyond that: I find the name of that method; 除此之外:我找到了该方法的名称; and how it deals with that boolean field ... a bit confusing. 以及它如何处理该布尔字段...有点令人困惑。 You see, if encryption is true ... the method does nothing?! 您会看到,如果加密是正确的 ,则该方法不执行任何操作? Then it doesn't "toggle" anything. 然后,它不会“切换”任何内容。 Thus that name is really misleading resp. 因此,这个名字确实令人误解。 not matching what your code is doing! 与您的代码不匹配!

Here charAt(i) returns a char: 在这里charAt(i)返回一个char:

sentence.charAt(i) += (char)x;

1) You cannot assign a character to a value but you can do it to a variable. 1)您不能将字符分配给值,但可以将其分配给变量。

2) Even if you used a variable such as 2)即使您使用了诸如

char tempChar = sentence.charAt(i);

You cannot do then : 您将无法执行以下操作:

tempChar += (char)x;

As you cannot increment (+=) a character with another character. 因为您不能将(+=)一个字符与另一个字符相加。

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

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