栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

三人身高问题

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

三人身高问题

1.需求分析:

测量三个人的身高,求最高的身高。

2.代码设计:

1、身高未知,通过键盘录入实现。

2、定义三个变量,用来存放测量到的身高值,不需要考虑单位问题。

3、可以利用三目运算符。

3.程序实现:

①Java实现:

import java.util.Scanner;

public class Demo2{
        public static void main(String[] args){
                Scanner sc = new Scanner(System.in);
                System.out.println("请输入第一个人的身高:");
                int height1 = sc.nextInt();
                System.out.println("请输入第二个人的身高:");
                int height2 = sc.nextInt();
                System.out.println("请输入第三个人的身高:");
                int height3 = sc.nextInt();
                int tempHeight = height1>height2?height1:height2;
                int maxHeight = tempHeight>height3?tempHeight:height3;
                System.out.println("三人中最高的身高:"+maxHeight+"cm");
        }
}

②C实现:

#include 
int main(){
	int height1,height2,height3;
    scanf("%d%d%d",&height1,&height2,&height3);
    int result=(height1>height2?height1:height2)>height3?(height1>height2?height1:height2):height3;
    printf("%d",result);
    return 0;
}

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/781249.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号