zoukankan      html  css  js  c++  java
  • 【Java学习日记】编程题整理-7.31 冒泡排序 -判断质数-十进制转十六进制-判断回文数 -获得反转数

      最近在学Java,教材是机械工业出版社的《Java语言程序设计》,辅佐廖雪峰的博客。

      把遇到的编程题记录在txt中不太方便写备注,改为在日记里记录还可以总结。


    冒泡排序

        public static void main(String[] args) {
          int ns[]= {28, 12, 89, 73, 65, 18, 96, 50, 8, 36};
          System.out.println("原始数列:"+ Arrays.toString(ns));
          for(int i =0;i<ns.length-1;i++) {
            for(int j=0;j<ns.length-1;j++) {
              if(ns[j]>ns[j+1]) {
                int tem = ns[j];
                ns[j] = ns[j+1];
                ns[j+1] =tem;
                }
              }
            }
          System.out.println("排序以后:"+ Arrays.toString(ns));
        }

      冒泡排序的模板相对比较固定,用sort方法也可以输出从小到大排列的数列。

      获取数组长度使用方法 ns.length 其中ns是数组名


    判断质数

      public static void main(String[] args) {
        System.out.println("Please Enter a number");
        Scanner input = new Scanner(System.in);
        int n = input.nextInt();
        int count = 0;
        int number =2;
        final int line = 10;//控制每输出十个数就换行
        while(count<n) {
          if(Isprime(number)) {
            count++;
          if(count%line == 0) {
            System.out.println(number);
          }
          else {
            System.out.print(number + " ");
           }
        }
        number++;
      }

    }
        //判断质数的方法
        public static boolean Isprime(int i) {
          for(int divisor = 2;divisor <=i/2; divisor++) {
            if(i%divisor == 0) {
              return false;
            }
          }
          return true;
        }
    }


    十进制转十六进制

        public static void main(String[] args) {
          Scanner input = new Scanner(System.in);
          int decimal = input.nextInt();
          System.out.println("The value of the decimal number is " + decimaltoHex(decimal));
         }

        public static String decimaltoHex(int decimal) {
          String hex ="";
          while(decimal != 0) {
            int hexValue = decimal % 16;
          hex = toHexChar(hexValue)+ hex;   //转化成字符
          decimal=decimal/16;         
          }
         return hex;
        }

        public static char toHexChar(int hexValue) {
          if(hexValue<=9 && hexValue>=0) 
            return (char)(hexValue + '0');    
          else 
            return (char)(hexValue - 10 + 'A');

        }
    }


    判断是否是回文数

        public static void main(String[] args) {
          Scanner input = new Scanner(System.in);
          int n = input.nextInt();
          if(getNumbers(n)) {
            System.out.println("yes");
          }
          else
            System.out.println("no");
          }

        public static boolean getNumbers(int n) {
          String str = String.valueOf(n);   //数值转化为字符串
          int length = str.length();            //获得字符串长度
          int L=length-1,R=0;      //L代表字符串最左边,length-1  R代表是字符的最左边,从0开始
          if(length == 1)
            return true;
          while (L > R) {
            if (str.charAt(L) != str.charAt(R)) {      //不管奇数偶数都要最高位和最低位比起来 获得字符串里第N位字符的方法 str.charAt(N)
              return false;
            }
            L--;
            R++;
          }
           return true;
         }

    }


    获得反转数

        public static void main(String[] args) {
          Scanner input = new Scanner(System.in);
          int n = input.nextInt();
          while (n>0){
            System.out.print(n%10);    // 取余
          n /= 10;      //相当于每次砍掉最低位
          }
         }


    好的,今天就整理这些。

  • 相关阅读:
    SpringBoot自动装配源码
    对称加密、非对称加密、数字签名
    k8s部署mysql数据持久化
    docker部署 springboot 多模块项目+vue
    ES入门及安装软件
    prometheus入门介绍及相关组件、原理讲解
    流水线 Sonar 代码扫描
    postgresql数据库 查询表名、备注及字段、长度、是否可控、是否主键等信息
    Helm中Tiller镜像下载失败的解决办法
    程序员孔乙己
  • 原文地址:https://www.cnblogs.com/JodyJoy1201/p/13413287.html
Copyright © 2011-2022 走看看