简体   繁体   English

降价算法Java; 转换器问题

[英]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 ("");
    }     
}

Ok, so now I have an algorithm that converts # to < h1> < h2> depending on the number of #...I'm now trying to add < p> at the beginning of a paragraph and < /p> at the end of it. 好的,现在我有了一个算法,可以根据#的数量将#转换为<h1> <h2>。我现在尝试在段落的开头添加<p>,在</ p>处添加结束了。 For some reason,the second part of the converter, which is supposed to add < p> at the beginning and < /p> at the end of a paragraph doesnt seem to work (it's the code starting with double carac). 出于某种原因,转换器的第二部分(应该在段落的开头添加<p>并在段落的末尾添加</ p>)似乎不起作用(这是从double carac开头的代码)。 Can someone tell me what I'm doing wrong??? 有人可以告诉我我在做什么错吗???

You are printing two opening tags for a paragraph if the string starts with an uppercase letter and no closing tag. 如果字符串以大写字母开头且没有结束标记,则您正在为一个段落打印两个开始标记。 Replace 更换

System.out.print("<p>");
System.out.println(str);
System.out.println("<p>");

with

System.out.print("<p>");
System.out.println(str);
System.out.println("</p>"); //<--here

Also, you should use a logical AND && instead of a bitwise AND & for boolean operations. 另外,对于布尔运算,应使用逻辑AND &&而不是按位AND &

Also, String#charAt(int) returns a char , not a double . 另外, String#charAt(int)返回一个char ,而不是double You are declaring carac as a double . 您将carac声明为double Declare as a char instead. 声明为char

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM