TÓPICO

Está dando wrong answer, alguém pode me ajudar?

Atlans159 perguntou 1 year ago

import java.io.IOException; import java.util.Scanner;

public class Main {

public static void main(String[] args) throws IOException {

    Scanner ler = new Scanner(System.in);

    System.out.println("Informe sua idade em dias: ");
    int idade = ler.nextInt();
    int ano = idade / 365;
    int mes = ((ano % 365) / 30);
    int dia = ((ano % 365) % 30);

    if(idade >= 0) { 
        System.out.printf("%d ano(s) \n%d mes(es) \n%d dia(s)", 
        ano, mes, dia);
    }
    ler.close();
}

}

Lembre de não publicar soluções. Sua publicação pode ser revisada por nossos moderadores.

  • Tahsin09 respondido 1 year ago

    import java.io.IOException;
    
    import java.util.Scanner;
    
    public class Main{
        public static void main(String[]args){
            Scanner sc = new Scanner(System.in);
            int n = sc.nextInt();
            System.out.println(n/365 + " ano(s)");
            n = n%365;
            System.out.println(n/30 + " mes(es)");
            n = n%30;
            System.out.println(n + " dia(s)");
       }
    }
    
  • www.jonas-silva.dev respondido 1 year ago

    O correto seria não usar a classe Scanner como no código abaixo:

    import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader;

    public class Main { public static void main(String[] args) throws IOException { InputStreamReader ir = new InputStreamReader(System.in); BufferedReader in = new BufferedReader(ir);

        int dias = Integer.parseInt(in.readLine());
    
        int anos = dias/365;
        dias %= 365;
        int meses = dias/30;
        dias %= 30;
    
        System.out.printf("%d ano(s)\n", anos);
        System.out.printf("%d mes(es)\n", meses);
        System.out.printf("%d dia(s)\n", dias);
    }

    }