Exercise
Formats
Objetive
Write a java program to ask the user for a number and display it four times in a row, separated with blank spaces, and then four times in the next row, with no separation. You must do it two times: first using Console.Write and then using {0}.
Example:
Enter a number: 3
3 3 3 3
3333
3 3 3 3
3333
Example Code
import java.util.*;
public class Main
{
public static void main(String[] args)
{
int n;
System.out.println("Enter a digit: ");
n = Integer.parseInt(new Scanner(System.in).nextLine());
// Part A: "n n n n" using Write
System.out.print(n);
System.out.print(" ");
System.out.print(n);
System.out.print(" ");
System.out.print(n);
System.out.print(" ");
System.out.print(n);
System.out.println();
// Part B: "nnnn" using Write
System.out.print(n);
System.out.print(n);
System.out.print(n);
System.out.println(n);
System.out.println();
// Part C: "n n n n" using {0}
System.out.printf("%1$s %1$s %1$s %1$s" + "\r\n", n);
// Part D: "nnnn" using {0}
System.out.printf("%1$s%1$s%1$s%1$s" + "\r\n", n);
}
}