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

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

分析以下需求,并用代码实现:
        (1)键盘录入三个整数,按照从小到大的顺序输出
        (2)如果用户输入的是3 2 1,程序运行后打印格式"按照从小到大排序后的顺序为:1 2 3"

import java.util.Scanner;
public class Test1 {
                                public static void main(String[] args) {
                                        //1.创建键盘录入对象
                                        Scanner sc = new Scanner(System.in);

                                        //2.通过键盘录入输入三个整数
                                        System.out.println("请输入第一个整数:");
                                        int x = sc.nextInt();

                                        System.out.println("请输入第二个整数:");
                                        int y = sc.nextInt();

                                        System.out.println("请输入第三个整数:");
                                        int z = sc.nextInt();

                                        int temp;
                                        //3.将x,y,z中的最小数存入到x中
                                        if(x>y) {
                                                temp = x;
                                                x = y;
                                                y = temp;
                                        }

                                        if(x>z) {
                                                temp = x;
                                                x = z;
                                                z = temp;
                                        }

                                        //4.将y,z中的最小数存入到y中
                                        if(y>z) {
                                                temp = y;
                                                y = z;
                                                z = temp;
                                        }
                                       //输出结果
                                        System.out.println("x="+x+",y="+y+",z="+z);
                                }
                        }

5 个回复

倒序浏览
感觉你这样有点麻烦了,直接把他们放进集合中 ,然后倒序循环输出   这个是我今天的作业题,给你参考下package Test1;

import java.util.Scanner;

/*编写程序,将键盘录入的字符串倒序打印,并测试
*/
public class Test2 {

        public static void main(String[] args) {
                Scanner sc = new Scanner(System.in);
                System.out.println("请输入一个字符串");
                String s = sc.nextLine();
                char[] ch = s.toCharArray();
                        for (int i =ch.length-1; i>=0 ; i--) {
                                System.out.print(ch[i]);
                        }
                }
               

        }

回复 使用道具 举报
  1. public class Main {

  2.         public static void main(String[] args) {
  3.                 Scanner sc = new Scanner(System.in);
  4.                 String str = sc.nextLine();
  5.                 sc.close();
  6.                 String[] s = str.split(" ");
  7.                 int[] num = new int[s.length];
  8.                 for (int i = 0; i < s.length; i++) {
  9.                         num[i] = Integer.parseInt(s[i]);
  10.                 }

  11.                 Arrays.sort(num);
  12.                 for (int i : num) {
  13.                         System.out.println(i);
  14.                 }
  15.         }
  16. }
复制代码
回复 使用道具 举报
你可以直接放数组中,用sort()方法排序输出
回复 使用道具 举报
代码很清晰,注释也很好
回复 使用道具 举报
可以有,学习了
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马