how to write a java program for printing child or adult in java

‮al.www‬utturi.com
how to write a java program for printing child or adult in java

To write a Java program for printing "child" or "adult" based on a given age, you can use an if-else statement or a switch statement.

Here is an example of how to use an if-else statement to print "child" or "adult" based on a given age in Java:

int age = 18;
if (age < 18) {
  System.out.println("child");
} else {
  System.out.println("adult");
}

In this example, the if statement checks if the value of the age variable is less than 18. If the condition is true, the program prints "child". If the condition is false, the program prints "adult".

You can customize the age limit by changing the value of the age variable or the comparison operator in the if statement. You can also add additional conditions and branches to the if-else statement to handle other cases, such as "teenager" or "elderly".

Here is an example of how to use a switch statement to print "child" or "adult" based on a given age in Java:

int age = 18;
switch (age) {
  case 0:
  case 1:
  case 2:
  case 3:
  case 4:
  case 5:
  case 6:
  case 7:
  case 8:
  case 9:
  case 10:
  case 11:
  case 12:
  case 13:
  case 14:
  case 15:
  case 16:
  case 17:
    System.out.println("child");
    break;
  default:
    System.out.println("adult");
}

In this example, the switch statement compares the value of the age variable to a list of constant values. If the value of age matches one of the constants, the program prints "child". If the value of age does not match any of the constants, the program prints "adult".

You can customize the age limit by changing the values of the constants or adding additional cases to the switch statement. You can also use the break statement to exit the switch statement after printing the appropriate label.

You can use these approaches to write a Java program that prints "child" or "adult" based on a given age. You can also use other approaches, such as using a function or a method to determine the label based on the age, or using a data structure, such as an array or a map, to store the labels and the age limits.

Created Time:2017-11-01 22:29:48  Author:lautturi