Java程序,用于按人的身高对高、矮和中等进行分类
在这个程序中,按照一个人的身高对高、矮和平均进行分类。为了进行比较,将标准参考高度考虑为 151 厘米至 175 厘米。 151cm 到 175cm 范围内的身高属于平均身高。身高超过 175 厘米属于较高身高类别,身高低于 150 厘米属于矮人类别。
例子:
Input: 164
Output: This person has Average height
Input: 180
Output: This person is taller
执行:
Java
// Java Program to Categorize Taller,
// Dwarf and Average by Height of a Person
class Height_classifier {
public static void main(String[] args)
{
int height = 180;
if (height > 175) {
// prints that person is taller
System.out.println("This person is Taller");
}
else if (height > 150 && height <= 175) {
// prints that person has average height
System.out.println(
" This person has Average height");
}
else {
// prints that person is a graph
System.out.println(" This person is Dwarf");
}
}
}
Java
// Java Program to Categorize Taller,
// Dwarf and Average by Height of a Person
class Height_classifier {
public static void main(String[] args)
{
System.out.println(
"Enter the height in centimeters:");
// taking input from user using command line
int height = Integer.parseInt(args[0]);
if (height > 175) {
// prints that person is taller
System.out.println("This person is Taller");
}
else if (height > 150 && height <= 175) {
// prints that person has average height
System.out.println(
" This person has Average height");
}
else {
// prints that person is a graph
System.out.println(" This person is Dwarf");
}
}
}
输出
This person is Taller
使用命令行参数实现:
Java
// Java Program to Categorize Taller,
// Dwarf and Average by Height of a Person
class Height_classifier {
public static void main(String[] args)
{
System.out.println(
"Enter the height in centimeters:");
// taking input from user using command line
int height = Integer.parseInt(args[0]);
if (height > 175) {
// prints that person is taller
System.out.println("This person is Taller");
}
else if (height > 150 && height <= 175) {
// prints that person has average height
System.out.println(
" This person has Average height");
}
else {
// prints that person is a graph
System.out.println(" This person is Dwarf");
}
}
}
输出: