import java.io.*;
public class Carddelete
{
public static void main(String[] args)throws IOException
{
StringBuffer yp=new StringBuffer("A234567890JQK");
System.out.println("Let's play now!!");
//注意其中的0代表10
StringBuffer p=new StringBuffer("A234567890JQK");
System.out.println(p);
int deletenum=0;
while(deletenum<13)
{
char dec=p.charAt(9); //p字符串中10号牌
int i;
int sign=0;
for(i=0;i<yp.length();i++)
if(yp.charAt(i)==dec)
{
sign=i;
yp.deleteCharAt(i);//删除 yp(原)字符串中的牌
break;
}
deletenum++;
if(deletenum==12)
break;
StringBuffer s=new StringBuffer(yp.substring(sign)); //新建字符串 s包含 yp串中未读过的
int len2=s.length(); //s 串的长度
int len1=yp.length(); //剩余 yp串的字符个数
int n=((10-len2)/len1)+1;
s.append(yp);
for(i=1;i<n;i++)
s.append(yp );
//扩展字符串s
p=s;
}
System.out.println(yp);
}
}
本文介绍了一个使用Java实现的简单卡片删除程序。该程序通过循环从两组相同的牌组中逐张删除相同的卡片,并展示了如何利用StringBuffer进行字符串操作。文章通过具体的代码示例解释了这一过程。
1167

被折叠的 条评论
为什么被折叠?



