黑马程序员技术交流社区

标题: 使用if语句 [打印本页]

作者: 小b,试试就试试    时间: 2016-7-24 22:45
标题: 使用if语句
分析以下需求,并用代码实现:
        (1)键盘录入三个整数,按照从小到大的顺序输出
        (2)如果用户输入的是3 2 1,程序运行后打印格式"按照从小到大排序后的顺序为:1 2 3"

import java.util.Scanner;
class Zuo1{
        public static void main(String[] args) {
                Scanner sc = new Scanner(System.in);
               
                System.out.println("请输入第一个整数");
                int x  = sc.nextInt();
                System.out.println("请输入第二个整数");
                int y  = sc.nextInt();
                System.out.println("请输入第三个整数");
                int z  = sc.nextInt();
               
                if (x>y){
                        if(x>z){
                                if(y>z){
                                System.out.println("从小到大排序后的顺序为:"+ z +" "+ y +" "+ x);
                                }else{
                                System.out.println("从小到大排序后的顺序为:"+ y +" "+ z +" "+ x);
                                }
                        }else{
                                System.out.println("从小到大排序后的顺序为:"+ y +" "+ x +" "+ z);
                        }
                }
                        else if(y > z){
                                if(x > z){
                                System.out.println("从小到大排序后的顺序为:"+ z +" "+ x +" "+y );
                                }else{
                                System.out.println("从小到大排序后的顺序为:"+ x +" "+ z +" "+y );
                                }
                        }else{
                        System.out.println("从小到大排序后的顺序为:"+ x +" "+ y +" "+ z);
                        }
                               
        }
}

作者: 1208124957    时间: 2016-7-24 23:17
public static void main(String[] args) {
                Scanner sc = new Scanner(System.in);
                while(true){
                        System.out.println("输入三个数字,以空格隔开");
                        int a = sc.nextInt();
                        int b = sc.nextInt();
                        int c = sc.nextInt();
                        if(a > b ){
                                int temp = a;
                                a = b;
                                b = temp;
                        }
                        if(a > c ){
                                int temp = a;
                                a = c;
                                c = temp;
                        }
                        if(b > c){
                                int temp = b;
                                b = c;
                                c = temp;
                        }
                        System.out.println("从小到大的顺序为:"+a+" "+b+" "+c);
                }
        }
作者: 15105106710    时间: 2016-7-25 00:02
前排的真是高手呀,没有一定逻辑能力搞不定呀
作者: 阿卜    时间: 2016-7-25 14:01
建议使用数组,然后使用sort排序来输出代码比较简洁。
作者: 小b,试试就试试    时间: 2016-7-25 22:29
阿卜 发表于 2016-7-25 14:01
建议使用数组,然后使用sort排序来输出代码比较简洁。

刚学4天
作者: 小b,试试就试试    时间: 2016-7-25 22:31
阿卜 发表于 2016-7-25 14:01
建议使用数组,然后使用sort排序来输出代码比较简洁。

刚学没几天,数组还在后面呢,不过以后可以试试吧
作者: huangsong1002    时间: 2016-7-25 22:35
import java.util.Scanner;
                class  Test10 {
                        public static void main(String[] args) {
                                Scanner sc = new Scanner (System.in);
                                System.out.println("请输入第一个整数");
                        int x = sc.nextInt();
                                System.out.println("请输入第二个整数");
                        int y = sc.nextInt();
                                System.out.println("请输入第三个整数");
                        int z = sc.nextInt();
                        int a = (((x > y) ? y : x) > z) ? z : ((x > y) ? y : x);
                        int c = (((x > y) ? x : y) > z) ?  ((x > y) ? x : y) : z;
                        int b = x ^ y ^ z ^ a ^ c;
                        System.out.println("按照从小到大排序后的顺序为:" + a + b + c);
                }
        }

作者: LiuWei2015    时间: 2016-7-25 22:41
哈哈代码还可以大大优化哦




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