A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 重头再来_0809 中级黑马   /  2015-8-31 00:01  /  517 人查看  /  1 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

使用三元运算符完成如下练习
        比较两个数是否相等
        获取两个数中最大值
        获取三个数中最大值

        我把他们一起了
  1. import java.util.Scanner;
  2. class text9 {
  3.         public static void main(String[] args) {
  4.                 Scanner sc = new Scanner(System.in);
  5.                 System.out.println("请输入一个整数");
  6.                 int x = sc.nextInt();
  7.                 System.out.println("请再次输入一个整数");
  8.                 int y = sc.nextInt();
  9.                 System.out.println("请再次输入一个整数");
  10.                 int z = sc.nextInt();
  11.                 System.out.println();
  12.                 int a;

  13.                 bj(x,y); //比较两个数是否相等
  14.                 a = max(y,z); //求后两个数最大值
  15.                 System.out.println("后两个数中的最大值是:"+a);
  16.                 a = max(x,y,z); //求最大值
  17.                 System.out.println("三个数的最大值是:" + a);

  18.         }
  19.         public static void bj(int x,int y) {
  20.                 System.out.println("前两个数"+((x==y) ? "相等" :"不相等"));
  21.         }
  22.         public static int max(int x,int y) {
  23.                 int temp = x > y ? x : y;
  24.                 return temp;
  25.         }
  26.         public static int max(int x,int y,int z) {
  27.                 int temp;
  28.                 temp = max(max(x,y),z);
  29.         return temp ;       
  30.         }
  31. }
复制代码


集 三目运算、键盘输入、重载、引用于一体。

1 个回复

正序浏览
顶一下!!!
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马