多个变量的 Java System.out.println 语法帮助

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/25451719/
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-11 00:25:58  来源:igfitidea点击:

Java System.out.println syntax help for multiple variables

java

提问by query

I'm new to java and trying to print the values of multiple variables.But the quotes inside the System.out.printlnconfusing me.Can anybody explain the following syntax?

我是java新手并试图打印多个变量的值。但是里面的引号让我 System.out.println困惑。有人可以解释以下语法吗?

Why "+ b1.cc"is outside the quotes ?

为什么 "+ b1.cc"在引号之外?

My code:

我的代码:

System.out.println("Bike data " + b1.brand + " " + b1.color + " " + b1.cc);

采纳答案by Frakcool

Let's say you have:

假设你有:

String one = "1";
String two = "2";
String three = "3";

System.out.println("one: " + stringOne + " and two: " + stringTwo + " and also a three: " + stringThree);

Will print

会打印

one: 1 and two: 2 and also a three: 3

It is called concatenation. I.e. you "create a new String".

它被称为串联。即你“创建一个新的字符串”。

Look at this answer toofor mor information.

也请查看此答案以获取更多信息。

In you actual code " "will just add a white space between the values of your variables.

在您的实际代码中,您" "只会在变量值之间添加一个空格。

回答by Patrick Cookson

The quotes create a String object for the JVM to use. The variables:

引号创建了一个供 JVM 使用的 String 对象。变量:

b1.brand

b1.brand

b1.color

b1.color

b1.cc

b1.cc

will return a String object already so the quotes aren't necessary. If, for instance, b1.colorwas in quotes, it would print specifically b1.color and not what the variable holds.

将已经返回一个 String 对象,因此不需要引号。例如,如果b1.color是在引号中,它将专门打印 b1.color 而不是变量所包含的内容。

回答by markspace

I think you need to learn about string concatenation in Java. You can call a method to concatenate (join together) two strings, but you can also use the +operator.

我认为您需要了解 Java 中的字符串连接。您可以调用一个方法来连接(连接在一起)两个字符串,但您也可以使用+运算符。

The String class includes a method for concatenating two strings:

String 类包括一个连接两个字符串的方法:

string1.concat(string2);

string1.concat(string2);

This returns a new string that is string1 with string2 added to it at the end.

这将返回一个新字符串,它是 string1,最后添加了 string2。

You can also use the concat() method with string literals, as in:

您还可以将 concat() 方法与字符串文字一起使用,如下所示:

"My name is ".concat("Rumplestiltskin");

"我的名字是".concat("Rumplestiltskin");

Strings are more commonly concatenated with the + operator, as in

字符串更常与 + 运算符连接,如

"Hello," + " world" + "!"

"Hello," + " world" + "!"

which results in

这导致

"Hello, world!"

“你好,世界!”

The + operator is widely used in print statements. For example:

+ 运算符广泛用于打印语句。例如:

String string1 = "saw I was ";
System.out.println("Dot " + string1 + "Tod");

which prints

哪个打印

Dot saw I was Tod

点看到我是托德

Such a concatenation can be a mixture of any objects. For each object that is not a String, its toString() method is called to convert it to a String.

这种串联可以是任何对象的混合。对于每个不是 String 的对象,调用其 toString() 方法将其转换为 String。

回答by Elliott Frisch

You have presented an example of String concatenation, equally valid would be building a String reference separately like,

您已经提供了一个字符串连接的示例,同样有效的是单独构建一个字符串引用,例如,

String str = "Bike data " + b1.brand + " " + b1.color + " " + b1.cc;
System.out.println(str);

Java also supports formatted printing. Assuming those fields are all String(s) you could use

Java 还支持格式化打印。假设这些字段都是您可以使用的字符串

 System.out.printf("Bike data %s %s %s", b1.brand, b1.color, b1.cc);

or String.format()

或者 String.format()

 String str = String.format("Bike data %s %s %s", b1.brand, b1.color, b1.cc);