把一个字符串通过递归转换成相应数字

package WildCat.XML.readFloas;

import java.io.File;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

public class changeToString {
	public static int num=0;
	public static StringBuffer sb= new StringBuffer();
	//getDocumentName getName=new getDocumentName();
	
	static ArrayList<String> list = new ArrayList<String>();
	//static ArrayList<String> list = new getDocumentName().getName("RAR\\WildCat\\ok\\8\\oh.ppt");
	
	/**
	 * @param args
	 * 
	 * 通过DOM4J读取XML
	 * @throws Exception 
	 */
	
	public static void test(String firName,Element e) throws Exception
	{		
		for (Iterator iter=e.elementIterator();iter.hasNext();)
			{			
				Element ele=(Element)iter.next();
				String name=ele.attributeValue("name");
				if (firName.equals(name))
				{
					String id=ele.attributeValue("id");
					System.out.println("Id:"+id);
					num++;
					sb.append(id+"\\");
					
					if (num<list.size())
					{
						test(list.get(num),ele);
					}
				}
				
		     }	
	}
	
	public static String change(String firstPath ) throws Exception {
		getDocumentName getName=new getDocumentName();
		list = new getDocumentName().getName(firstPath);
		
		SAXReader reader=new SAXReader();
		Document document=reader.read(new File("RAR.xml"));
		Element root=document.getRootElement();
		if (list.get(0).equals(root.getName())) 
		{
			
			num++;
			test(list.get(num),root);
		}
		String path=sb.toString();
		//清空全局变量StringBuffer
		sb=null;		
		return path;
	}
	//测试主函数
	public static void main(String[] args) throws Exception {
		String tt=change("RAR\\jim\\ok\\8\\oh.ppt");
		System.out.println(tt);
	}

}
对应的xml文件:

<?xml version="1.0" encoding="UTF-8"?>

<jim>
  <first1 name="你好.doc" id="3"/>
  <first1 name="ok" id="1">
    <first1 name="oh.ppt" id="2"/>
    <first1 name="8" id="1">
      <first1 name="myGod.doc" id="1"/>
      <first1 name="oh.ppt" id="2"/>
    </first1>
    <first1 name="second" id="3">
      <first1 name="mytest.doc" id="1"/>
      <first1 name="null" id="2"/>
    </first1>
  </first1>
  <first1 name="z" id="2">
    <first1 name="mytest.doc" id="1"/>
    <first1 name="test.doc" id="2"/>
  </first1>
</jim>

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
### 回答1: 可以使用递归法将一个整数n转换成字符串,具体步骤如下: 1. 将整数n除以10,得到商和余数。 2. 如果商为,说明n已经被转换完毕,直接返回余数对应的字符。 3. 如果商不为,递归调用函数,将商转换成字符串,并将余数对应的字符添加到字符串末尾,最后返回字符串。 代码如下: ``` def int_to_str(n): if n < 10: return str(n) else: return int_to_str(n // 10) + ' ' + str(n % 10) n = 345 s = int_to_str(n) print(s) # 输出:3 4 5 ``` ### 回答2: 递归法将一个整数n转换字符串是一种通过拆分整数的位数,并递归地将每个位数转换字符串的方法。下面是一个递归函数的实现: ``` def int_to_str(n): # 递归终止条件:当n小于10时,直接返回n的字符串形式 if n < 10: return str(n) else: # 将n的除个位数外的其余位数转换字符串并与个位数拼接返回 return int_to_str(n // 10) + " " + str(n % 10) ``` 通过递归地将n除以10,直到n小于10,然后将每次的余数(个位数)与之前递归得到的结果(除个位数外的其余位数的字符串)拼接,就可以获得整数n的字符串形式。 测试示例: ``` print(int_to_str(345)) # 输出:"3 4 5" print(int_to_str(1234567890)) # 输出:"1 2 3 4 5 6 7 8 9 0" print(int_to_str(7)) # 输出:"7" ``` 以上是该问题的解答。 ### 回答3: 递归法将一个整数n转换成字符串的方法,可以通过对整数n进行递归拆分得到每位数字的方式实现。 具体步骤如下: 1. 首先判断n是否为0,若为0则直接返回空字符串。 2. 若n不为0,则进行以下步骤: a. 取n的个位数,记为digit。 b. 调用递归函数将n除以10得到的商,记为new_n,然后将new_n转换成字符串,记为str_n。 c. 将digit和str_n拼接起来,得到新的字符串,记为result。 d. 返回result。 举个例子,假设输入的整数为345。 1. 由于345不为0,则继续执行步骤2。 a. 取345的个位数字5。 b. 将345除以10得到的商为34,调用递归函数将34转换成字符串,得到"3 4"。 c. 将5和"3 4"拼接起来,得到结果"5 3 4"。 d. 返回"5 3 4"作为最终结果。 通过递归的方式,我们可以将任意位数的整数n转换成对应的字符串表示。 需要注意的是,该方法只适用于非负整数的转换,若输入的整数可能为负数,需要加入相应的判断逻辑和处理过程。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值