Java 递归识别
我想知道如何解决需要通过使用递归从文件中读取文本(包含java代码)来正确缩进文本的问题。原始输出将没有任何 标签 这就是目标: 这是代码,但我需要修改它以从文本文件读取:Java 递归识别,java,recursion,Java,Recursion,我想知道如何解决需要通过使用递归从文件中读取文本(包含java代码)来正确缩进文本的问题。原始输出将没有任何 标签 这就是目标: 这是代码,但我需要修改它以从文本文件读取: void indent( int m, int n ) { System.out.println( m ); // Forward Printing if ( m < n ) { indent( m + 1, n ); System.out.println( m ); // Backwar
void indent( int m, int n )
{
System.out.println( m ); // Forward Printing
if ( m < n )
{
indent( m + 1, n );
System.out.println( m ); // Backward Printing
}
}
void缩进(int m,int n)
{
System.out.println(m);//正向打印
if(m
好的,算法可以是
function indent(theText, indentCharCount) {
for each character of the text...
if it is an end-of-line character...
concatenate spaces to return string using indentCharCount
else if it is a '{' character...
scan ahead through characters to find matching '}' character
recursively call indent() function passing...
characters between { and } for 'theText' param
indentCharCount+2 for 'indentCharCount' param
concatenate return value of indent() to return string
set loop index so that the next character will be the matched '}'
else (it's some other character)
concatenate character to return string
return the concatenated string
}
我不想用Java写出所有的代码。如果你在做家庭作业,我宁愿你学点东西!但我认为这是一个基本的递归算法,适合你的问题。使用
astyle
、indent
或ide
。大部分(全部)都能做到。@Elliott必须是递归的。你试过什么(展示a)?你卡在哪里了,有什么问题吗?如果文本
是读卡器
的一个实例,你不需要提前扫描,因为当递归调用返回时,读卡器将位于右括号后的字符处。非常有用,先生!我要试着实现它。