任务描述
本关任务:接收给定的字符串,往字符串的括号中添加数字。具体要求如下: 1.往第一个括号中依次添加8-12,第二个括号中对应添加18-22; 2.输出每个新字符串。
测试输入:
#content_views > p:nth-child() > child()
预期输出:
#content_views > p:nth-child(8) > child(18)
#content_views > p:nth-child(9) > child(19)
#content_views > p:nth-child(10) > child(20)
#content_views > p:nth-child(11) > child(21)
#content_views > p:nth-child(12) > child(22)
import java.util.Scanner;
public class StrTest {
public static void main(String[] args) {
// 请在Begin-End间编写代码
/********** Begin **********/
// 接收给定的字符串
Scanner nb=new Scanner(System.in);
String s1=nb.nextLine();
int w,w2,e,e2;
int q=8;
// 创建StringBuffer对象
// 添加数字并输出添加数字后的字符串
if(s1.startsWith("("))
{s1=s1.substring(1);
for(int i=0;i<5;i++){
StringBuffer s=new StringBuffer(s1);
w=s.indexOf("(");
s.insert(w+1,q);
w2=s.indexOf(")");
e=s.lastIndexOf("(");
s.insert(e+1,q+10);
e2=s.lastIndexOf(")");
System.out.println("("+s);
q++;}}
else{
for(int i=0;i<5;i++){
StringBuffer s=new StringBuffer(s1);
w=s.indexOf("(");
s.insert(w+1,q);
w2=s.indexOf(")");
e=s.lastIndexOf("(");
s.insert(e+1,q+10);
e2=s.lastIndexOf(")");
System.out.println(s);
q++;}
}
/********** End **********/
}
}
/*
任务:接受给定的字符串,往字符串的括号中添加数字。具体要求如下:
1.往第一个括号中添加8-12,第二个括号中对应添加18-22;
2.输出每个新字符串。
给定字符串样式:#content_views > p:nth-child() > child()
输出样式:#content_views > p:nth-child(8) > child(18)
*/
import java.util.Scanner;
public class StrTest {
public static void main(String[] args) {
// 请在Begin-End间编写代码
/********** Begin **********/
// 接收给定的字符串
Scanner scanner = new Scanner(System.in);
String next = scanner.nextLine();
// 创建StringBuffer对象
StringBuffer stringBuffer1 = new StringBuffer(next);
// 添加数字并输出添加数字后的字符串
for(int n=8;n<10;n++){
stringBuffer1.insert(29, n).insert(40, n + 10);
System.out.println(stringBuffer1);
stringBuffer1.delete(29,30).delete(39,41);
}
for(int m=10;m<13;m++){
stringBuffer1.insert(29, m).insert(41, m + 10);
System.out.println(stringBuffer1);
stringBuffer1.delete(29,31).delete(39,41);
}
/********** End **********/
}
}