用Eclipse 编程 以下程序代码 (求各位大神帮忙)
发布网友
发布时间:2023-04-23 02:10
我来回答
共1个回答
热心网友
时间:2023-10-08 16:27
public class Format {
public enum E_Format{
format,
WarpFormat,
SeqFormat,
HtmlFormat
}
class PrintFormat
{
public void print(String s, E_Format f,int c)
{
String rs = "";
switch(f)
{
case format:
rs = format(s,c);
break;
case WarpFormat:
rs = WarpFormat(s,c);
break;
case SeqFormat:
rs = SeqFormat(s,c);
break;
case HtmlFormat:
rs = HtmlFormat(s,c);
break;
default:
rs = s;
break;
}
p(rs);
}
}
public void Test(String s,int c)
{
PrintFormat pf = new PrintFormat();
pf.print(s, E_Format.format, c);
pf.print(s, E_Format.WarpFormat, c);
pf.print(s, E_Format.SeqFormat, c);
pf.print(s, E_Format.HtmlFormat, c);
}
/**
* @param args
*/
public static void main(String[] args) {
//p("Oh , no no no?\n\n");
Format f = new Format();
String s = "test and print text!";
f.Test(s,5);
}
public static void p(String s)
{
System.out.println(s);
}
public String format(String s,int c)
{
StringBuilder sb = new StringBuilder();
if (c < s .length())
{
for( int i=0; i<s.length(); i+=c)
{
int e = i + c;
if (e>=s.length()){ e = s.length()-1;}
sb.append(s.substring(i, e));
sb.append("\n");
}
}else{ sb.append(s);}
return sb.toString();
}
public String SeqFormat(String s,int c)
{
StringBuilder sb = new StringBuilder();
if (c < s .length())
{
int idx = 1;
for( int i=0; i<s.length(); i+=c)
{
int e = i + c;
if (e>=s.length()){ e = s.length()-1;}
sb.append("("+idx+")");
sb.append(s.substring(i, e));
sb.append("\n");
idx++;
}
}else{ sb.append(s);}
return sb.toString();
}
public String HtmlFormat(String s,int c)
{
StringBuilder sb = new StringBuilder();
if (c < s .length())
{
int idx = 1;
for( int i=0; i<s.length(); i+=c)
{
int e = i + c;
if (e>=s.length()){ e = s.length()-1;}
sb.append("<span>");
sb.append(s.substring(i, e));
sb.append("</span>\n");
idx++;
}
}else{ sb.append(s);}
return sb.toString();
}
public void addChar(StringBuilder sb,char f,int c,Boolean bLine)
{
for(int i=0;i<c;i++) sb.append(f);
if(bLine)sb.append("\n");
}
public String WarpFormat(String s,int c)
{
StringBuilder sb = new StringBuilder();
if (c < s .length())
{
addChar(sb,'*',c+2,true);
for( int i=0; i<s.length(); i+=c)
{
int e = i + c;
if (e>=s.length()){ e = s.length()-1;}
sb.append("*");
sb.append(s.substring(i, e));
if (e-i<c){ addChar(sb,' ', c-(e-i),false);}
sb.append("*\n");
}
addChar(sb,'*',c+2,true);
}else{ addChar(sb,'*',c+2,true);
sb.append(s);addChar(sb,'*',c+2,true);
}
return sb.toString();
}
}