Como imprimir a série

3 respostas
javaprogramação
L

Seja a seguinte série: 1, 4, 4, 2, 5, 5, 3, 6, 6, 4, 7, 7, … Escreva um programa que seja capaz de
gerar os seus N termos

3 Respostas

J

Qual a dúvida?

D

Além da duvida, o que você fez?

N
import java.util.Scanner;

public class Serie {
    public static void main(String... args)
    {
        int total;
        int valorExibido;

        System.out.print("Digite o total de termos: ");
        total = (new Scanner(System.in)).nextInt();

        for(int indice = 1, outroIndice = 0, numeroDaSequencia = 1; indice <= total; indice++, outroIndice++)
        {
            valorExibido = numeroDaSequencia + 2;

            if(outroIndice > 2)
            {
                outroIndice = 0;
            }

            if (outroIndice == 0)
            {
                valorExibido = numeroDaSequencia;
                numeroDaSequencia++;
            }

            System.out.print(" " + valorExibido + ", ");
        }
    }
}
Criado 23 de setembro de 2022
Ultima resposta 19 de out. de 2022
Respostas 3
Participantes 4