简体   繁体   English

Java StringBuilder字符追加返回不需要的数字

[英]Java StringBuilder character appending returns unwanted number

I am very sorry if this is a basic question which has been answered before (I tried looking but I did not find anything) 非常抱歉,如果这是一个之前已经回答的基本问题(我尝试查找但没有找到任何东西)

I am trying to write the following Java method: 我正在尝试编写以下Java方法:

 String winningCard(String trick, char trump) {
    StringBuilder sb = new StringBuilder();
    char suit;
    char rank;
    for(int i = 1;   i < trick.length(); i+=2) {
        if(trick.charAt(i) == trump) {
            suit = trick.charAt(i);
            rank = trick.charAt(i-1);
            sb.append(rank + suit); //issue here, returns a weird number
            break;
        }
    }
    String result = sb.toString();
    return result;
}

When called with these arguments "8s7hQd", 'h' for example, it is supposed to return "7h". 当使用这些参数“ 8s7hQd”(例如“ h”)调用时,应返回“ 7h”。

If I change the StringBuilder to only append either the suit or the rank, it does it just fine, but if I put it the way it is above it returns "159" which I believe has something to do with the unicode encoding. 如果我将StringBuilder更改为仅追加西装或等级,它就可以了,但是如果按上面的方式放置它,它会返回“ 159”,我认为这与Unicode编码有关。

I'd very much appreciate if a kind sould could tell me what I am missing. 如果一个善良的灵魂能够告诉我我所缺少的,我将非常感激。

Thanks in advance 提前致谢

suit and rank are basically numbers. suitrank基本上是数字。 The + is adding these numbers and appending it. +将这些数字相加并附加。

If you place a "" between, the chars will be appended as you intend, because it forces the compiler to use the + with a String. 如果在两者之间放置"" ,则字符将按预期的方式追加,因为它会强制编译器将+与字符串一起使用。

sb.append(rank + "" + suit);
append(rank).append(suit);

应该做的把戏

+ is a tricksy thing, because it means different things in different contexts. +是一件棘手的事情,因为在不同的上下文中它意味着不同的事情。

  • If at least one of the operands is a String , it acts as the string concatenation operator. 如果至少一个操作数是String ,则它充当字符串连接运算符。
  • If both of the operands are numbers, or convertible to numbers via unboxing, then it acts as the numeric addition operator. 如果两个操作数都是数字,或者可以通过拆箱转换为数字,则它将用作数字加法运算符。

You are giving it two char s: these are numbers, so numeric addition occurs. 您给它两个char :它们是数字,因此会出现数字加法。

Before adding the two chars, they are widened to int ; 在添加两个字符之前,将它们扩展为int the result is an int too. 结果也是一个int And it is this int that you are appending to the string builder, hence the "unwanted" number. 这就是您要附加到字符串生成器的int ,因此是“不需要的”数字。

So, either avoid using the addition operator at all (best): 因此,要么完全避免使用加法运算符(最好):

sb.append(rank).append(suit);

Or make sure you are using the string concatenation operator: 或确保您使用的是字符串串联运算符:

sb.append("" + rank + suit);
// Left-associative, so evaluated as
// ("" + rank) + suit

sb.append(String.valueOf(rank) + suit);
// Etc.

But actually, you don't need to do either: just append the substring: 但是实际上,您不需要执行任何操作:只需添加子字符串:

sb.append(trick, i-1, i+1);

This extracts a portion of the trick string, as trick.substring(i-1, i+1) would, but does it without creating a new string. 这将提取trick字符串的一部分,就像trick.substring(i-1, i+1)一样,但是不会创建新的字符串。

And you don't need a loop 而且您不需要循环

您可以直接说这些字符应通过以下方式解释为String

sb.append(String.valueOf(rank) + String.valueOf(suit))

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

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