csp 2017_3_3 markdown

题目连接


package csp2017_3_3;

import java.util.Scanner;

public class Main {

	public static void main(String[] args) {
		Scanner in = new Scanner(System.in);
		String line, preline;
		line = null; preline = null;
		int start, end;
		String text = "";
		while(in.hasNextLine()) {
			line = in.nextLine();
			if( line == null || line.length() == 0) {
				if(preline == null)//跳过多个空行
					continue;
				
				if(preline.contains("<h")) {
					System.out.print(preline);
				}
				else if(preline.contains("<ul>")) {
					System.out.print(preline + "\n" + "</ul>" + "\n");
				}
				else if(preline.contains("<p>")) {
					System.out.print(preline+ "</p>" + "\n");
				}
				preline = null;//输出当前模块后 需要置 
				continue;
			}
			//强调   @两种写法                   
//            while((start = line.indexOf("_")) != -1){
//                end = line.indexOf("_",start+1);
//                text = line.substring(start, end+1);
//                line = line.replace(text,"<em>"+text.substring(1,text.length()-1)+"</em>");
//            }
            //超链接
//            while((start = line.indexOf("[")) != -1){
//                end = line.indexOf("]",start+1);
//                text = line.substring(start+1, end);
//                int khstart = line.indexOf("(",end);
//                int khend = line.indexOf(")",khstart);
//                String link = line.substring(khstart+1,khend);
//                line = line.replace("["+text+"]"+"("+link+")","<a href=\""+link+"\""+">"+text+"</a>");//注意 href=后面没有空格
//           }

			while((start = line.indexOf("_")) != -1) {
				end = line.indexOf("_",start+1);
				String tmp = "";
				tmp += "<em>" + line.substring(start+1,end) + "</em>";
				line =  line.substring(0,start) + tmp + line.substring(end+1);
			}
			while((start = line.indexOf("["))!= -1) {
				end = line.indexOf("]",start+1);
				String tex = line.substring(start+1,end);
				int tStart = line.indexOf("(",end);
				int tEnd = line.indexOf(")",tStart);
				String link = line.substring(tStart+1,tEnd);
				line = line.substring(0,start) + "<a href=\""+ link + "\"" + ">" + tex+ "</a>"  + line.substring(tEnd+1);
			}
			
			int num = 0;
			if(line.startsWith("#")) {
				while(line.startsWith("#") && num <= 6) {
					num++;
					line = line.substring(1);
				}
					
				while(line.startsWith(" ")) {
				//	line.replaceFirst(" ", ""); line本身没变 陷入死循环
					line = line.replaceFirst(" ", "");
				}
				preline = "<h"+num+ ">"+line + "</h" + num+">" + "\n";
			}
			else if(line.startsWith("*")) {
				line = line.replace("*", "");
				while(line.startsWith(" "))
					line = line.substring(1);
				if(preline == null) {
					preline = "<ul>"+"\n"+"<li>"+line+"</li>";
				}
				else {
					preline += "\n" + "<li>" + line + "</li>";
				}
			}
			else {
				if(preline == null) {
					preline = "<p>" + line;
				}
				else {
					preline += "\n" +line;
				}
			}	
		}//end while
		if(preline.contains("<h")) {//注意 sys.out println()带的空格
			System.out.print(preline);
		}
		else if(preline.contains("<ul>")) {
			System.out.print(preline + "\n" + "</ul>" + "\n");
		}
		else if(preline.contains("<p>")) {
			System.out.print(preline+ "</p>" + "\n");
		}

	}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值