'I can't compile the rest after inputed the age in eclipse, it says "Terminated" on console. Does anyone know how to solve the problem ? Thanks
'What i am trying to do:: Show message based on
Good morning (12am-12pm) Good after noon (12pm -4pm) Good evening (4pm to 9pm) Good night ( 9pm to 6am)'
import java.util.Calendar;
import java.util.GregorianCalendar; import java.util.Scanner;
public class inputers {
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);
}
}
}
Solution 1:[1]
You will have to wait until the hour of day is greater than 15, because all you output happens in the else if (hour<=24) case.
Or you could move the output out of this code block:
} 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);
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Thomas Kläger |
