Java 我如何连接 int 值而不添加它们?(爪哇)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22517074/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 16:12:38  来源:igfitidea点击:

How so I concatenate int values without adding them? (Java)

javastringcoding-styleconcatenation

提问by noobiecderssss

New at coding, and I'm running some basic exercises to get used to the language. In this exercise I'm trying to generate a phone number with the following restrictions:

编码新手,我正在运行一些基本练习以习惯该语言。在本练习中,我尝试生成具有以下限制的电话号码:

  1. 1st 3 digits cannot contain an 8 or 9
  2. 2nd set of 3 digits cannot be higher than 742
  1. 前 3 位数字不能包含 8 或 9
  2. 第二组 3 位数字不能高于 742

I've seen suggestions to add an empty string (which I have), but I don't understand why that works. For now, I'll be sticking with the following, even though I don't fully understand why it works.

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

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);

EDIT: NEW CODE INCLUDES sb.append

编辑:新代码包括 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 's answer worked for me. Though it does seem to require more coding with all the sb.append calls I have to include. I've yet to learn about the StringBuilder class, but it definitely seems to be helpful. Thank you, everyone.

@Serge 的回答对我有用。虽然它似乎需要更多的编码来处理我必须包含的所有 sb.append 调用。我还没有了解 StringBuilder 类,但它似乎很有帮助。谢谢大家。

采纳答案by Serge

Use a stringbuilder

使用字符串生成器

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());

回答by Sam Barnum

The + operator behaves differently for Strings and numbers. When invoked on a String, it concatenates. When invoked on a numeric type, it adds. By starting with "", you are telling the compiler to concatenate a number to the string, which in turn converts the number to a String. This is repeated for all further concatenations.

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

回答by StreamingBits

The problem is that you are literally adding the num1, num2, and num3 etc. You need to do convert it to a string to concatenate. An easy way would be to do this.

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

   String no1 = Integer.toString(num1);
   String no2 = Integer.toString(num2); 

//DO THAT FOR EVERY numX and then your code will work

An alternative, is String builder

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

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);

回答by Frakcool

here's the explanation:

这是解释:

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

It concatenates them because we're adding them into a String (text variable) instead of int (number variable)

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

The way they're adding the numbers is correct

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

Yeah if you add "" to the beggining the compiler will parse from int to String (I don't know why), but you can change it to:

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

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

Or:

或者:

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

回答by The amateur programmer

In your solution ints are first changed to string representations of numbers and then they are concatenated to an empty String. This isn't a good practice because of the fact that Strings are immutable.

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

Another way to accomplish this is by using a string builder to append the integer values as Stringrepresentations into its buffer with StringBuilder.append(int)method. Then you can get the resulting string with StringBuilder.toString().

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

回答by Nimesh

Here you are adding blank quote in starting of String to mention that you want to perform string operation.

在这里,您在 String 的开头添加空白引号以提及您要执行字符串操作。

"+" operator works differently here.

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

If your string concatenation finds 2 numbers in starting of operation it will add num1 with num2.

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

For example,

例如,

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

That's why you need to put blank quote.

这就是为什么你需要输入空白报价。

String is immutable so it is good choice to use StringBuilder for String Concatenation.

字符串是不可变的,因此使用 StringBuilder 进行字符串连接是不错的选择。

回答by user5081873

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???