'我要做的事情::显示基于的消息
早上好(上午12点至下午12点(中午过后(中午12点-下午4点(晚上好(下午4点至9点(晚安(晚上9点至早上6点(’
import java.util.Calendar;
导入java.util.GregorianCalendar;导入java.util.Scanner;
公共类输入程序{
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner input = new Scanner(System.in);
GregorianCalendar time = new GregorianCalendar();
int hour = time.get(Calendar.HOUR_OF_DAY);
int min = time.get(Calendar.MINUTE);
int day = time.get(Calendar.DAY_OF_MONTH);
int month = time.get(Calendar.MONTH) + 1;
int year = time.get(Calendar.YEAR);
String name, address;
int age;
String greeting;
System.out.print("Name: ");
name = input.nextLine();
System.out.print("Address: ");
address = input.nextLine();
System.out.print("age: ");
age = input.nextInt();
if(hour>=1 && hour<=11){
greeting = "Morning";
} else if(hour<=15){
greeting = "Afternoon";
} else if(hour<=24){
greeting = "Evening";
System.out.println(" ");
System.out.println("Good " + greeting + ", " + name);
System.out.println("are you sure that you're " + age + " years ?" );
System.out.println("and live on " + address);
}
}
}
您将不得不等到一天中的小时数大于15,因为所有输出都发生在else if (hour<=24)
的情况下。
或者你可以将输出移出这个代码块:
} else if(hour<=24) {
greeting = "Evening";
} else {
greeting = "Night";
}
System.out.println(" ");
System.out.println("Good " + greeting + ", " + name);
System.out.println("are you sure that you're " + age + " years ?" );
System.out.println("and live on " + address);