什么是Java相当于在C#中打印值?

pri*_*Joe 8 c# java printing

在C#中,我可以说:

 int myInt = 10;
 int myInt2 = 20;
 Console.WriteLine("My Integer equals {0}, and the other one equals {1}", myInt, myInt2);
Run Code Online (Sandbox Code Playgroud)

而且我知道如何用Java打印这样的东西:

 int myInt = 10;
 System.out.println("My Integer equals " + myInt);
Run Code Online (Sandbox Code Playgroud)

那么如何将这两者结合起来,以便在Java中,我可以像在C#中一样打印多个值?

Ser*_*rvy 16

你可以String.format明确地打电话:

System.out.println(String.format(
    "My Integer equals %d, and the other one equals %d", myInt, myInt2));
Run Code Online (Sandbox Code Playgroud)


小智 9

别忘了这些printf陈述..

int myInt = 0;
int myInt2 = 20;

System.out.printf("My Integers equal %d, and the other one equals %d\n",
                               myInt, myInt2);
Run Code Online (Sandbox Code Playgroud)


Ank*_*agi 2

您可以用来+显示多个变量

int myInt = 10;
int myInt2 = 20;
System.out.println("My Integer equals " + myInt 
                    + "and second integer is " + myInt2);
Run Code Online (Sandbox Code Playgroud)

  • @therealtbs:展示替代解决方案没有任何问题。 (3认同)