Issue
I am figuring how to print the results of 4 integers on separate lines by typing only one System.out.println
(I guess typing less is better for coders).
Expected Output :
43
1
19
13
My code:
class JavaApplication1 {
public static void main(String[] args) {
int a,b,c,d;
a = -5 + 8 * 6;
b = (55+9) % 9;
c = 20 + -3*5 / 8;
d = 5 + 15 / 3 * 2 - 8 % 3 ;
System.out.println(a);
System.out.println(b);
System.out.println(c);
System.out.println(d);
}
}
Solution
You can do i with several ways :
This will print the variable and between each a new-line char
:
System.out.println(a + "\n" + b + "\n" + c + "\n" + d);
You can also use method reference and Arrays
, this will create a dynamic List from the array composed of your 4 variables, and apply System.out::println
to each one :
Arrays.asList(a,b,c,d).forEach(System.out::println);
or
IntStream.of(a,b,c,d).forEach(System.out::println);
Use a basic for each loop
can also be a way :
for (int i : new int[]{a, b, c, d})
System.out.println(i);
Print with format
also :
System.out.printf("%d%n%d%n%d%n%d%n",a,b,c,d)
Answered By - azro Answer Checked By - Clifford M. (PHPFixing Volunteer)
0 Comments:
Post a Comment
Note: Only a member of this blog may post a comment.