[英]markdown algorithm java; converter problems
public class Md2html {
public static void main(String[] args) throws IOException {
String stringToConvert = new Scanner(System.in).nextLine();
System.out.println(convert(stringToConvert));
}
public static String convert(String str) {
if (str.equals("# "))
System.out.println(" ");
Pattern pattern = Pattern.compile("(#+[^#]+)");
Matcher matcher = pattern.matcher(str);
while (matcher.find()) {
String str1 = matcher.group(1);
if(str1.replaceFirst("#+", "").length() == 0 ||
str1.replaceFirst("#+", "").matches("[\\s]+"))
continue;
int n = str1.length() - str1.replaceFirst("#+", "").length();
System.out.println("<h" + n + ">" + str1.substring(n) +
"</h" + n + ">");
double carac;
carac = str.charAt(0);
if(carac>65 & carac <=90) {
System.out.print("<p>");
System.out.println(str);
System.out.println("<p>");
}
}
return ("");
}
}
好的,现在我有了一个算法,可以根据#的数量将#转换为<h1> <h2>。我现在尝试在段落的开头添加<p>,在</ p>处添加结束了。 出于某种原因,转换器的第二部分(应该在段落的开头添加<p>并在段落的末尾添加</ p>)似乎不起作用(这是从double carac开头的代码)。 有人可以告诉我我在做什么错吗???
如果字符串以大写字母开头且没有结束标记,则您正在为一个段落打印两个开始标记。 更换
System.out.print("<p>");
System.out.println(str);
System.out.println("<p>");
与
System.out.print("<p>");
System.out.println(str);
System.out.println("</p>"); //<--here
另外,对于布尔运算,应使用逻辑AND &&
而不是按位AND &
。
另外, String#charAt(int)
返回一个char
,而不是double
。 您将carac
声明为double
。 声明为char
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.