+ 1

How I can print two var in one println?

12th Jan 2018, 11:46 AM
Maryam
Maryam - avatar
13 Respostas
+ 2
int a=5,b=10; System.out.println("var1="+a+" var2="+b); output: var1=5 var2=10
12th Jan 2018, 11:56 AM
Hrishikesh Kulkarni
Hrishikesh Kulkarni - avatar
+ 2
With printf you can format what you want to print a bit easier than with println. Example: String firstName = "John"; String lastName = "Doe"; int age = 41; System.out.println("Name: " + firstName + " " + lastName + "\n" + Age: " + age); System.out.printf("Name: %s %s\nAge: %d\n", firstName, lastName, age); As you can see, the second is a bit shorter and easier to read. But the true power of printf is the formatting. - %d is a format specifier for integer values. In the above example is used for the age variable. It tells to the printf method, that there should be an integer in the arguments. And it should be at the right place: In the example we have two strings (the names), and then the age variable: %s %s %d => the variables should be in the same order: firstName, lastName, age \n is escape character to go on the next line But more about what the format specifiers can do, look here: https://sharkysoft.com/archive/printf/docs/javadocs/lava/clib/stdio/doc-files/specification.htm
12th Jan 2018, 2:32 PM
Boris Batinkov
Boris Batinkov - avatar
+ 1
in which language?
12th Jan 2018, 11:55 AM
sal sal
sal sal - avatar
+ 1
in which language?
12th Jan 2018, 11:55 AM
sal sal
sal sal - avatar
+ 1
if u asking about java. int a=1; int b=2; System. out.println(a+" "+b);
12th Jan 2018, 11:59 AM
sal sal
sal sal - avatar
+ 1
Yes I hope it will work @maryam
12th Jan 2018, 11:59 AM
Hrishikesh Kulkarni
Hrishikesh Kulkarni - avatar
+ 1
int a = 5; int b = 10; System.out.println(a + " " + b); // 5 10 // You can use the print format method - printf // %s - Strings, %d - Integers, %f - Float, \n - for new line System.out.printf("%d %d\n", a, b); // 5 10
12th Jan 2018, 12:19 PM
Boris Batinkov
Boris Batinkov - avatar
+ 1
Thank you so much
12th Jan 2018, 2:37 PM
Maryam
Maryam - avatar
+ 1
%d, %f and %d are placeholders , google if for more info
13th Jan 2018, 12:07 PM
Ibrahim Osama
Ibrahim Osama - avatar
+ 1
نعم وضحت الاجابة هي بمثابة تخصيص مكان للمتغيرات ...شكرا لكم جميعا
13th Jan 2018, 12:53 PM
Maryam
Maryam - avatar
0
I will try it ^^
12th Jan 2018, 11:58 AM
Maryam
Maryam - avatar
0
sal sal in jave
12th Jan 2018, 11:58 AM
Maryam
Maryam - avatar
0
B G @ what is the different between printf and println and why you use % d
12th Jan 2018, 1:14 PM
Maryam
Maryam - avatar