Movatterモバイル変換


[0]ホーム

URL:


Skip to content
DEV Community
Log in Create account

DEV Community

Beatriz Maciel
Beatriz Maciel

Posted on • Edited on

     

HackerRank #27 | 1D Array | 🇧🇷

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]);        }    }}
Enter fullscreen modeExit fullscreen mode

=========

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();}
Enter fullscreen modeExit fullscreen mode

=========

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();
Enter fullscreen modeExit fullscreen mode

============

Essa publicação faz parte de uma série de exercícios resolvidos em Java no HackerRank. Acesse a série completa:

Top comments(0)

Subscribe
pic
Create template

Templates let you quickly answer FAQs or store snippets for re-use.

Dismiss

Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment'spermalink.

For further actions, you may consider blocking this person and/orreporting abuse

Software Developer
  • Work
    Vilt Group
  • Joined

More fromBeatriz Maciel

DEV Community

We're a place where coders share, stay up-to-date and grow their careers.

Log in Create account

[8]ページ先頭

©2009-2025 Movatter.jp