如何在Java中将两个变量的输出显示在同一行?

huangapple go评论79阅读模式
英文:

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

huangapple
  • 本文由 发表于 2020年9月22日 22:40:50
  • 转载请务必保留本文链接:https://go.coder-hub.com/64012124.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定