RenzCutie

ArrayTraversalII

Oct 4th, 2022
42
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.86 KB | None | 0 0
  1. import java.util.Scanner;
  2. public class Main{
  3.  
  4. public static void main(String[] args) {
  5. Scanner scan = new Scanner(System.in);
  6.  
  7. System.out.print("Enter size: ");
  8. int sz = scan.nextInt();
  9.  
  10. int arr[] = new int[sz];
  11.  
  12. int len = 0;
  13. for (int i = 0; i < sz; i++) {
  14. System.out.printf("Enter element %d: ", i +1);
  15. int tmpInt = scan.nextInt();
  16.  
  17. if (tmpInt <= 0)
  18. break;
  19. len = i;
  20. arr[i] = tmpInt;
  21.  
  22. }
  23.  
  24. for (int i = 0; i <= len; i++) {
  25. if (i == 0 )
  26. System.out.print("[");
  27. System.out.print(arr[i]);
  28.  
  29. if (i == len )
  30. System.out.print("]");
  31. else
  32. System.out.print(",");
  33.  
  34.  
  35. }
  36.  
  37. }
  38. }
  39.  
Advertisement
Add Comment
Please, Sign In to add comment