码迷,mamicode.com
首页 > 编程语言 > 详细

入门算法递归+循环+异常处理练习2020010301

时间:2020-01-03 12:00:18      阅读:63      评论:0      收藏:0      [点我收藏+]

标签:++   oid   imm   img   arch   temp   static   异常处理   com   

package org.jimmy.autosearch.test;

import java.util.ArrayList;
import java.util.HashMap;

public class Recursive2020010301 {

    public static void main(String[] args) {
        try {
            ArrayList<HashMap<String, Integer>> list = new ArrayList<HashMap<String, Integer>>();
            HashMap<String, Integer> map = new HashMap<String, Integer>();
            int maxNumber = 3;
            int numberCount = maxNumber - 1;
            int initialIndex = 1;
            int initialIndex2 = 0;
            map.put("maxNumber", maxNumber);
            map.put("numberCount", numberCount);
            map.put("initialIndex", initialIndex);
            map.put("initialIndex2", initialIndex2);
            list.add(map);
            map = new HashMap<String, Integer>();
            maxNumber = 4;
            numberCount = maxNumber - 1;
            initialIndex = 1;
            initialIndex2 = 0;
            map.put("maxNumber", maxNumber);
            map.put("numberCount", numberCount);
            map.put("initialIndex", initialIndex);
            map.put("initialIndex2", initialIndex2);
            list.add(map);
            int currentIndex = 0;
            test5(list, currentIndex);
        } catch (Exception e) {
            
        }
    }
    
    public static void test5(ArrayList<HashMap<String, Integer>> list, int currentIndex) throws Exception {
        HashMap<String, Integer> map = list.get(currentIndex);
        Integer maxNumber = (Integer) map.get("maxNumber");
        Integer numberCount = (Integer) map.get("numberCount");
        Integer initialIndex = (Integer) map.get("initialIndex");
        Integer initialIndex2 = (Integer) map.get("initialIndex2");
        try {
            test4(maxNumber, numberCount, initialIndex, initialIndex2);
        } catch (Exception e) {
            System.out.println("---------------------------------");
            currentIndex++;
            test5(list, currentIndex);
        }
    }
    
    public static void test4(int max, int count, int index, int index2) throws Exception {
        for(int i = index2; i < max; i++){
            int tempIndex = index;
            int tempIndex2 = i;
            int j = i + tempIndex;
            if(index2 >= max - 1 && j >= max - 1){
                throw new RuntimeException();
            }
            System.out.println("i:" + i + ",j:" + j);
            if(j >= max - 1){
                tempIndex = 0;
                tempIndex2++;
            }
            tempIndex++;
            if(i <= max - 1 && j <= max - 1){
                test4(max, count, tempIndex, tempIndex2);
            }
        }
    }

}

效果图:

技术图片

 

入门算法递归+循环+异常处理练习2020010301

标签:++   oid   imm   img   arch   temp   static   异常处理   com   

原文地址:https://www.cnblogs.com/JimmySeraph/p/12144067.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!