【问题标题】:How so I concatenate int values without adding them? (Java) [duplicate]我如何连接 int 值而不添加它们? (Java)[重复]
【发布时间】:2014-04-26 08:38:05
【问题描述】:

编码新手,我正在做一些基本练习以适应这种语言。 在本练习中,我尝试生成具有以下限制的电话号码:

  1. 前 3 位数字不能包含 8 或 9
  2. 第二组 3 位数不能高于 742

我看到了添加空字符串的建议(我有),但我不明白为什么会这样。现在,我将坚持以下内容,即使我不完全理解它的工作原理。

num1 = rand.nextInt(7) + 1;
num2 = rand.nextInt(7) + 1;
num3 = rand.nextInt(7) + 1;
num4 = rand.nextInt(643) + 100;
num5 = rand.nextInt(1001) + 1000;

String number = "" + num1 + num2 + num3 + "-" + num4 + "-" + num5;

System.out.print("Your assigned phone number is: " + number);

编辑:新代码包括 sb.append

    StringBuilder sb = new StringBuilder();

    int num1, num2, num3, num4, num5;

    num1 = rand.nextInt(7) + 1;
    num2 = rand.nextInt(7) + 1;
    num3 = rand.nextInt(7) + 1;
    num4 = rand.nextInt(643) + 100;
    num5 = rand.nextInt(1001) + 1000;

    sb.append(num1);
    sb.append(num2);
    sb.append(num3);
    sb.append("-");
    sb.append(num4);
    sb.append("-");
    sb.append(num5);

    //String number = "" + num1 + num2 + num3 + "-" + num4 + "-" + num5;

    System.out.print("Your assigned phone number is: " + sb.toString());

@Serge 的回答对我有用。尽管它似乎确实需要对我必须包含的所有 sb.append 调用进行更多编码。我还没有了解 StringBuilder 类,但它似乎确实很有帮助。谢谢大家。

【问题讨论】:

  • 我在这个问题的正文中没有看到实际问题?
  • @neminem 他的问题是“为什么有效”,即(为什么在字符串编号的开头添加“”,打印每个值而不是添加 num1+num2 并打印总和)

标签: java string coding-style concatenation


【解决方案1】:
int mon = calendar.get(Calendar.MONTH);
    int year = calendar.get(Calendar.YEAR);
    int date = calendar.get(Calendar.DATE);
    int day = calendar.get(Calendar.HOUR_OF_DAY);
    int min = calendar.get(Calendar.MINUTE);
    int i = 0;


    if(mon<= 9)

         mon =""+i+mon ;

    String dates;
    if(date <= 9)
        dates = ""+'0'+date;

    int sec = calendar.get(Calendar.SECOND);
    String Time1 = date+"-"+mon+"-"+year+" "+day+":"+min+":"+sec;
    min=min+5;
    String Time2 = date+"-"+mon+"-"+year+" "+day+":"+min+":"+sec;

    Time1=Time1+"_"+Time2;

    ``System.out.println(date+"-"+mon+"-"+year+" "+day+":"+min+":"+sec);

    return Time1;

}



 mon =""+i+mon ; is not being accepted shwing error  chnage type of mon to String...Why???

【讨论】:

  • 你建议的 Naman String s2 = ""+ num1 + num2; //这将导致23。我在上面的代码中做到了 mon =""+i+mon ;不被接受 shwing 错误将 mon 类型更改为字符串...为什么?我需要将 0 与 mon 值连接起来。
【解决方案2】:

这里你在字符串的开头添加空引号来说明你要执行字符串操作。

“+”运算符在这里的工作方式不同。

如果您的字符串连接在操作开始时发现 2 个数字,它将添加 num1 和 num2。

例如,

int num1 = 2;
int num2 = 3;
String s = num1 +num2; //This will result in 5
String s2 = ""+ num1 + num2; //This will result in 23

这就是为什么你需要加上空白引号。

String 是不可变的,因此最好使用 StringBuilder 进行字符串连接。

【讨论】:

    【解决方案3】:

    解释如下:

    num1 = rand.nextInt(7) + 1; //This generates a random number between 1 and 7 (because rand.nextInt(n); returns a random number between 0 and n, then first number in a phone number can't be zero, that's why +1 is added.
    num4 = rand.nextInt(643) + 100; // This one generates a random number between 1 and 643 and add 100 (because it can be 0 - 99 but that don't give us a number of 3 digits), so we add 100 and it will give us a number of 3 digits.
    num5 = rand.nextInt(1001) + 1000; // returns a random number of 4 digits between 1000 and 1001, so basicaly: 1000 or 1001.
    
    String number = "" + num1 + num2 + num3 + "-" + num4 + "-" + num5; //this will be the example:
    
    number = "367-783-1001";
    System.out.println("Your assigned phone number is: " + number);
    Your assigned phone number is: 367-783-1001
    

    它将它们连接起来,因为我们将它们添加到 String(文本变量)而不是 int(数字变量)中

    他们添加数字的方式是正确的

    是的,如果您在开头添加“”,编译器将从 int 解析为 String(我不知道为什么),但您可以将其更改为:

    String number = String.valueOf(num1) + String.valueOf(num2) + String.valueOf(num3) + "-" + String.valueOf(num4) + "-" + String.valueOf(num5);
    

    或者:

    String number = Integer.toString(num1) + Integer.toString(num2) + Integer.toString(num3) + "-" + Integer.toString(num4) + "-" + Integer.toString(num5);
    

    【讨论】:

    • 那行不通。这是完全相同的问题。您正在添加整数,而不是在第一个代码示例中连接。查看 Sam Barnum 的帖子
    • 不@StreamingBits,你不是在添加整数,你是在连接......他说他是编码新手,我添加了对该代码如何工作的解释,以及为什么整数前的“”会逐个打印而不是添加它们
    • 有趣,不知道你可以做“”的事情。 +1
    【解决方案4】:

    在您的解决方案中,int 首先更改为数字的字符串表示形式,然后将它们连接到一个空的 String。这不是一个好的做法,因为 String 是不可变的。

    实现此目的的另一种方法是使用字符串构建器通过StringBuilder.append(int) 方法将整数值作为String 表示形式附加到其缓冲区中。然后你可以用StringBuilder.toString() 得到结果字符串。

    【讨论】:

      【解决方案5】:

      使用字符串生成器

      StringBuilder sb = new StringBuilder();
      
      sb.append(num1);
      sb.append(num2);
      sb.append(num3);
      sb.append("-");
      sb.append(num4);
      sb.append("-");
      sb.append(num5);
      
      
      System.out.println("Your phone number is: " + sb.toString());
      

      【讨论】:

      • String 是不可变的,因此使用 StringBuilder 是首选方法。
      • 是的,你是对的。由于 String 是不可变的,String 连接方法的效率和速度要低得多。
      • 通过“+”的字符串连接在后台使用了一个StringBuilder,所以如果你一次连接它们,显式创建一个StringBuilder/StringBuffer没有任何好处。 docs.oracle.com/javase/8/docs/api/java/lang/String.html
      【解决方案6】:

      问题在于您实际上是在添加 num1、num2 和 num3 等。您需要将其转换为要连接的字符串。一个简单的方法是做到这一点。

         String no1 = Integer.toString(num1);
         String no2 = Integer.toString(num2); 
      
      //DO THAT FOR EVERY numX and then your code will work
      

      另一种选择,是字符串生成器

      StringBuilder sb = new StringBuilder();
      sb.append("");
      sb.append(num1);
      sb.append(num2);
      sb.append(num3);
      sb.append("-");
      sb.append(num4);
      sb.append(num5);
      sb.append(num6);
      sb.append(num7);
      String phoneNumber = sb.toString();
      System.out.println("PHONE NUMBER" + phoneNumber);
      

      【讨论】:

        【解决方案7】:

        对于字符串和数字,+ 运算符的行为不同。当在 String 上调用时,它会连接。当在数字类型上调用时,它会添加。从"" 开始,您告诉编译器将一个数字连接到字符串,然后将数字转换为字符串。对于所有进一步的串联重复此操作。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2021-06-15
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2015-03-10
          相关资源
          最近更新 更多