黑马程序员技术交流社区

标题: 比较任意两个键盘录入的整数的大小(代码体现) [打印本页]

作者: aiheima    时间: 2016-1-8 00:53
标题: 比较任意两个键盘录入的整数的大小(代码体现)
键盘录入两个数据,返回两个数中的较大值;比较两个数是否相等
代码实现:
import java.util.Scanner;
class FunctionCompare
{
        public static void main(String[] args)
        {
                System.out.println("键盘录入两个数据");
                Scanner sc1 = new Scanner(System.in);
                int a = sc1.nextInt();
                Scanner sc2 = new Scanner(System.in);
                int b = sc2.nextInt();
                int max = getMax(a,b);//返回最大值
                System.out.println("max = " + max);
                String str = compare(a,b);
                System.out.println(str);
        }
        public static int getMax(int x,int y)
        {
                        return x > y ? x : y;
        }
        public static String compare(int x,int y)
        {
                return x == y ? x + "相等" + y : x + "不相等" + y;
        }
}






欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/) 黑马程序员IT技术论坛 X3.2