-2

'我想要做什么:: 显示消息基于

早上好 (12am-12pm) 中午之后好 (12pm -4pm) 晚上好 (4pm 到 9pm) 晚安 (9pm 到 6am)'

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);
                
        }

}

}

4

1 回答 1

0

您将不得不等到一天中的小时数大于 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);
于 2022-02-22T06:41:01.740 回答