zoukankan      html  css  js  c++  java
  • Java编程中常用到的编程思想

    求和变量思想:

    1.定义求和变量

    2.将要求和的数字相加存储到求和变量中

    public class SumTest{
        public static void main(String[] args) {
            //求1-100(包含1和100)的数字总和
            int sum = 0;
            for (int i = 1; i <= 100; i++) {
                sum += i ;
            }
            System.out.println("sum = " + sum);
        }
    }

    计数器思想:

    1.定义计数变量

    2.将变量放到需要计数的语句体中用来记录次数

    public class CountTest{
        public static void main(String[] args) {
            //统计1-100之间(包含1和100)的偶数出现的次数
            int count = 0;
            for (int i = 1; i <= 100; i++) {
                if (i%2==0) {
                    count ++;
                }
            }
            System.out.println("count = " + count);
        }
    }

    交换器思想:

    1.定义一个中间变量

    2.用变量来做交换数据的桥梁;

    public class TempTest{
        public static void main(String[] args) {
            //交换变量a和b的值
            int a = 10;
            int b = 20;
            int temp;
            temp = a;
            a = b;
            b = temp;
            System.out.println("a = " + a);
            System.out.println("b = " + b);
        }
    }

    双指针思想:

    1.定义两个变量

    2.将需要标记的数据用变量表示

    public class HelloWorld {
        public static void main(String[] args) {
            //数组反转
            //1.定义两个变量
            //2.start表示小索引
            //3.end表示大索引
            //4.小索引和大索引交换
            //5.小索引++,大索引--
            //6.小索引<大索引交换位置
            //7.小索引>=大索引停止交换
            int[] arr = {19, 28, 37, 46, 50};
            int start = 0;
            int end = arr.length-1;
            int temp = 0;
            while(start<end){
                temp = arr[start];
                arr[start]=arr[end];
                arr[end]=temp;
                start++;
                end--;
            }
            System.out.println(Arrays.toString(arr));
        }
    }
  • 相关阅读:
    CMDB运维开发项目
    Twisted使用和scrapy源码剖析
    scrapy爬虫框架
    rabbitmq:centos7安装与python调用
    github创建远程仓库
    git使用
    Python模块:paramiko
    centos7安装python3和Django后,ModuleNotFoundError: No module named '_sqlite3'
    21. java面向对象
    20. java面向对象
  • 原文地址:https://www.cnblogs.com/erlangha/p/12630581.html
Copyright © 2011-2022 走看看