鉴于出生日期,我必须显示他的生日还剩多少天,我有这个代码。
public class EjercicioJava {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
String nombre, papellido,sapellido,direccion,poblacion, foto;
Integer telefono;
Scanner entrada= new Scanner(System.in);
System.out.println("Introduce nombre");
nombre= entrada.nextLine();
System.out.println("Introduce 1er apellido");
papellido= entrada.nextLine();
System.out.println("Introduce 2o apellido");
sapellido= entrada.nextLine();
System.out.println("Introduce dirección");
direccion= entrada.nextLine();
System.out.println("Introduce población");
poblacion= entrada.nextLine();
System.out.println("Introduzca telefono");
telefono= entrada.nextInt();
System.out.println("Introduce foto");
foto= entrada.nextLine();
System.out.println("Usted se llama "+nombre+ " " + papellido + " " + sapellido + " ,con dirección " +direccion + " ,/n vive en " +poblacion+ " y cumple años dentro de " + meses + " meses y " + dias + " dias");
}
}
在这里我给你一个完整的方法,计算下一个生日还有多少个月和多少天,也可以确定生日是否是今天。
就如何打印或返回生日计算而言,方法就是这样,您可以对其进行改进。
它还处理可能的异常,例如错误的日期,以便应用程序不会在这些情况下停止。
要使用它就像这样做一样简单,让我们看几个日期的测试:
三个测试的输出:
测试代码
您可以在 Rextester 中看到一个代码测试:VER DEMO
我需要请求出生日期并将其从当前日期中减去,如下所示: