【问题标题】:add number to a character to make it another character将数字添加到字符以使其成为另一个字符
【发布时间】:2015-06-10 03:38:57
【问题描述】:

我想要一个字符,给它添加数字并得到另一个char(a+2=c)

    int leng,leng2;
    String word = "";
    String key = "";
    String enc ="";
    char x;
    char z;
    char tost;
    System.out.println("gimme word");
    word=in.next();
    System.out.println("gimme key");
    key=in.next();
    leng=word.length();
    leng2=key.length();

    for(int i=1;i<=leng;i++)
    {
        z=word.charAt(i-1);
        x=key.charAt(i-1);
        int plus=z + x;
        tost=(char)plus;
        enc=enc+tost;
        System.out.println(enc);
        System.out.println(tost);
        System.out.println((char)z);
        System.out.println((char)x);
        System.out.println(plus);
        System.out.println((char)plus);
    }

我希望它打印 c 并在我的代码中使用 charAt 来打印,因为我有一个完整的字符串,我尝试搜索许多解决方案,并尝试了很多方法,但它们都没有工作。

编辑:按要求启用完整代码 char plus的方式不起作用,说是错误

【问题讨论】:

  • Java 字符串是 Unicode/UTF-16 代码单元的计数序列,其中一个或两个编码一个代码点。虽然偏移代码点是有意义的,但结果不一定与字符相关联。我猜你想在有限的范围内操作,比如Basic Latin 字母,如果 +2 超出范围,则环绕。

标签: java ascii letters


【解决方案1】:

如果您想对整个字符串执行此操作,则可以使用 for 循环 -

String r= "abc"; 
int x=2;

for(i=0; i<r.length(); i++){
  char z= r.charAt(0);  
  int plus=z+x; 
  System.out.println((char)plus);
}  

查看System.out.println() 的类型转换。由于plusint,因此您必须将其显式转换为char

【讨论】:

    【解决方案2】:

    您需要char plus=z+x; 而不是plus=r+x;

    这会将字符 a 添加 2,从而生成字符 c

    你可以打印出来:

    System.out.println(plus);
    

    【讨论】:

      【解决方案3】:

      它甚至不应该编译。您还没有声明变量 plus。您还想将 2 添加到存储在 z 中的 r 的索引零处的字符。因此,您必须将 x 添加到 z。改为这样做

      String r = "abc";  
      int x = 2;
      char z = r.charAt(0);
      char plus = z + x;
      System.out.println(plus);
      

      【讨论】:

      • z=word.charAt(i-1); //z is char x=key.charAt(i-1); //x is char plus=z + x; //plus is int tost=(char)plus; // tost is char enc=enc+tost; //enc is String System.out.println(enc); System.out.println(tost); System.out.println((char)z); System.out.println((char)x);这是原码
      【解决方案4】:

      你的代码

      String r= "abc";  
      int x=2;
      char z= r.charAt(0);  
      plus=r+x                                       // wrong do int plus=z+x;  
      String plus2=(String)String.valueOf(plus);     //not required
      System.out.println(plus2);                     // plus not defined
      

      更正的代码

      String r= "abc";  
      int x=2;
      char z= r.charAt(0);  
      int plus=z+x;  
      System.out.println((char)plus);
      

      输出

      c
      

      Demo

      修改后

      只改变一行

      int plus=z + Character.getNumericValue(x);
      

      Demo

      【讨论】:

      • z=word.charAt(i-1); //z is char x=key.charAt(i-1); //x is char plus=z + x; //plus is int tost=(char)plus; // tost is char enc=enc+tost; //enc is String System.out.println(enc); System.out.println(tost); System.out.println((char)z); System.out.println((char)x);这是原码
      猜你喜欢
      • 1970-01-01
      • 2020-04-13
      • 2012-08-28
      • 1970-01-01
      • 2020-06-12
      • 1970-01-01
      • 2013-08-07
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多