Neste exercício devemos declarar um Array que lên
elementos.
O número de entrada é n e, depois disso, precisamos que oinput
peça a quantidade de números equivalente an
.
O código começa assim:
import java.util.*;public class Solution { public static void main(String[] args) { Scanner scan = new Scanner(System.in); int n = scan.nextInt(); // .. escreva o código aqui scan.close(); for (int i = 0; i < a.length; i++) { System.out.println(a[i]); } }}
=========
Para criar um novo array que só recebeint
(com o nomea
, como é imprimido no fora.length
) a sintaxe é a seguinte:
int[] a = new int[n]
A quantidade de inputs é relativa ao númeron
, portanto precisamos fazer a seguinte iteração:
for (int i = 0; i < n; i++){ a[i] = scan.nextInt();}
=========
O código final fica assim, dentro damain
:
Scanner scan = new Scanner(System.in); int n = scan.nextInt(); int[] a = new int[n]; for (int i = 0; i < n; i++){ a[i] = scan.nextInt(); } scan.close();
============
Essa publicação faz parte de uma série de exercícios resolvidos em Java no HackerRank. Acesse a série completa:
- HackerRank #6 | Scanner e End-of-file
- HackerRank #7 | Int to String / String to Int
- HackerRank #8 | Date and Time
- HackerRank #9 | Static Initializer Block
- HackerRank #10 | Currency Formatter
- HackerRank #11 | DataTypes
- HackerRank #12 | Strings Introduction
- HackerRank #13 | Substring Comparisons
- HackerRank #14 | Abstract Class
- HackerRank #18 | BigInteger
- HackerRank #19 | Loops II
- HackerRank #20 | String Reverse
- HackerRank #23 | Instanceof keyword
- HackerRank #26 | Generics
- HackerRank #27 | 1D Array
- HackerRank #28 | Anagrams
- HackerRank #33 | Arraylist
- HackerRank #34 | Exception Handling Try / Catch
- HackerRank #36 | Exception Handling
- HackerRank #37 | List
- HackerRank #38 | SubArray
- HackerRank #39 | HashSet
- HackerRank #40 | Java Dequeue
Top comments(0)
For further actions, you may consider blocking this person and/orreporting abuse