英文:
how to show two variables output in one line in java?
问题
Sytem.out.println("value of x , y : "+x +y);
Output will be "value of x,y : 1020" where x is 10 and y is 20
But I want to print this value of x,y : 10 20
英文:
Sytem.out.println("value of x , y : "+x +y);
Output will be "value of x,y : 1020" where x is 10 and y is 20
But I want to print this "value of x,y : 10 20"
答案1
得分: 2
除了字符串拼接,您还可以使用String.format
来完成这个操作:
System.out.println(String.format("x和y的值:%d %d", x, y));
或者使用System.out.printf
:
System.out.printf("x和y的值:%d %d%n", x, y);
英文:
Other than string concatenation , you can also use String.format
to do this:
System.out.println(String.format("value of x & y : %d %d" , x, y));
or System.out.printf
:
System.out.printf("value of x & y : %d %d\n" , x, y);
答案2
得分: 1
在 System.out.println
中,你只能打印 String
值,因此需要按以下方式对变量进行格式化为一个 string
。
System.out.println(String.format("x 和 y 的值:%d %d", x, y));
英文:
On System.out.println
, you can print only String
values so it is needed to format the variables to one string
as follows.
Sytem.out.println(String.format("value of x & y : %d %d", x, y));
答案3
得分: 0
你的代码有问题
正确的是:
System.out.println("x和y的值:" + x + y);
每个变量都必须有“+”才能与其他变量连接起来。
英文:
your code is wrong
This is the correct:
System.out.println("value of x & y: " + x + y);
every variable has to have the "+" to get concadenated with other.
答案4
得分: 0
Sure, here's the translation of the code parts:
你可以这样写 -
System.out.println("x和y的值:" + x + " " + y);
或者你可以这样写 -
System.out.print("x和y的值:" + x + " " + y);
英文:
You can write like this-
System.out.println("value of x,y"+x+" "+y);
or you write like this-
System.out.print("value of x,y"+x+" "+y);
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论