我有4个不同的字符串缓冲区,名为a
,b
,c
和d
。它们各自包含相同的值0123456789
。我有一个名为compguess
的字符串,它总是包含4位数字,如“2912”。我有一个名为result
的字符串,此字符串可能包含值+
,-
,null
或+
和-
符号,如{{ 1}}或++--
。此结果字符串可能包含最多4个值。
我想要做的是从匹配的StringBuffer(2912 = abcd)中删除char,如果结果字符串只包含“ - ”,如“ - ”,“ - ”,“---”或“ - ” - ”。但是我的代码有时会丢失,有时则不会(甚至结果只包含“ - ”)。我做错了什么?
以下是完整的代码:
++-
这是我的代码的相关部分;
public Randomnumber(){
sb = new StringBuffer("123456789");
sb2 = new StringBuffer("0123456789");
sb3 = new StringBuffer("0123456789");
a = new StringBuffer("123456789");
b = new StringBuffer("0123456789");
c = new StringBuffer("0123456789");
d = new StringBuffer("0123456789");
trys =0;
usernumber="";
x=0;
}
public String getNewrandomnumber() {
return mynewnumber;
}
public void setusernumber(String number) {
usernumber = number;
}
public String create4digitrandom(StringBuffer sb) {
Random rnd = new Random();
mysayi2 ="";
for(int i=0;i<4;i++){
random = rnd.nextInt(sb.length());
mysayi2 += Character.toString(sb.charAt(random));
sb.deleteCharAt(random);
}
String str = String.valueOf(mysayi2);
return str;
}
public String create2digitrandom(StringBuffer sb) {
mysayi2 ="";
for(int i=0;i<4;i++){
if(i<1){
mysayi2 += Character.toString(sb.charAt(0));
sb.deleteCharAt(0);
}else{
mysayi2 += "0";
}
}
String str = String.valueOf(mysayi2);
return str;
}
public void create3digitrandom() {
Random rnd = new Random();
mysayi3 ="";
for(int i=0;i<3;i++){
if(i<1){
random = 1+rnd.nextInt(9);
mysayi3 += Character.toString(sb2.charAt(random));
sb2.deleteCharAt(random);
}else{
random = rnd.nextInt(10-i);
mysayi3 += Character.toString(sb2.charAt(random));
sb2.deleteCharAt(random);
}
if(random2 > 1){
mysayi3 += Character.toString(sb2.charAt(random));
}
}
random = rnd.nextInt(3);
mysayi3 += Character.toString(mysayi3.charAt(random));
computernumber = String.valueOf(mysayi3);
}
public String sayisalla() {
if(trys<2){
mysayi4=create4digitrandom(sb);
}else if(trys>1 && trys<3){
mysayi4=create2digitrandom(sb);
}else{
mysayi4=computerthink2();
}
trys++;
computerresult = mysayi4+" "+comparenumbers(mysayi4,usernumber,0);
return computerresult;
}
public String comparenumbers(String number,String computernumber, int who) {
result ="";
fakecomputernumber = computernumber;
for(int i=0;i<4;i++){
if(number.charAt(i)==fakecomputernumber.charAt(i)){
result += "+";
char[] myNameChars = fakecomputernumber.toCharArray();
myNameChars[i] = 'x';
fakecomputernumber = String.valueOf(myNameChars);
char[] myNameChars2 = number.toCharArray();
myNameChars2[i] = 'y';
number = String.valueOf(myNameChars2);
}
}
for(int i=0;i<4;i++){
if(number.charAt(i)!=fakecomputernumber.charAt(i)&&fakecomputernumber.indexOf(number.charAt(i))!=-1){
result += "-";
char[] myNameChars = fakecomputernumber.toCharArray();
myNameChars[fakecomputernumber.indexOf(number.charAt(i))] = 'x';
fakecomputernumber = String.valueOf(myNameChars);
char[] myNameChars2 = number.toCharArray();
myNameChars2[i] = 'y';
number = String.valueOf(myNameChars2);
}
}
if(result ==""){
result += "miss";
}else if(result =="++++"){
result = "Congratz you won!!!";
}
if(who ==0){
computerthink(number,fakecomputernumber,result);
}
return result;
}
public void computerthink(String compguess, String mynumber,String result) {
for(int i =0;i<4;i++){
remainingnumbers += Character.toString(sb3.charAt(i));
}
if(result ==""){
for(int i=0;i<4;i++){
if(remainingnumbers.indexOf(compguess.charAt(i))!=-1){
sb3.deleteCharAt(remainingnumbers.indexOf(compguess.charAt(i)));
}
if(i==0){
a.deleteCharAt(a.indexOf(Character.toString(compguess.charAt(i))));
}else if(i==1){
b.deleteCharAt(b.indexOf(Character.toString(compguess.charAt(i))));
}else if(i==2){
c.deleteCharAt(c.indexOf(Character.toString(compguess.charAt(i))));
}else if(i==3){
d.deleteCharAt(d.indexOf(Character.toString(compguess.charAt(i))));
}
}
}else if(result !="++++" && result.length()==4){
lengthofremaining = remainingnumbers.length();
for(int i =0;i<lengthofremaining;i++){
if(compguess.indexOf(remainingnumbers.charAt(i))!=-1){
sb3.deleteCharAt(i-x);
x++;
}
}
}else if(result =="++++"){
result = "You lost loser!!!";
}else if(result.indexOf("+")==-1 && result.indexOf("-")!=-1){
for(int i =0;i<4;i++){
if(i==0){
if(a.indexOf(Character.toString(compguess.charAt(i))) !=-1){
a.deleteCharAt(a.indexOf(Character.toString(compguess.charAt(i))));
}
}else if(i==1){
if(b.indexOf(Character.toString(compguess.charAt(i))) !=-1){
b.deleteCharAt(b.indexOf(Character.toString(compguess.charAt(i))));
}
}else if(i==2){
if(c.indexOf(Character.toString(compguess.charAt(i))) !=-1){
c.deleteCharAt(c.indexOf(Character.toString(compguess.charAt(i))));
}
}else if(i==3){
if(d.indexOf(Character.toString(compguess.charAt(i))) !=-1){
d.deleteCharAt(d.indexOf(Character.toString(compguess.charAt(i))));
}
}
}
}
}
public String computerthink2() {
generatednumber ="";
for(int i=0;i<4;i++){
Random rnd2 = new Random();
if(i==0){
randombetter = rnd2.nextInt(a.length());
generatednumber += a.charAt(randombetter);
}else if(i==1){
randombetter = rnd2.nextInt(b.length());
generatednumber += b.charAt(randombetter);
}else if(i==2){
randombetter = rnd2.nextInt(c.length());
generatednumber += c.charAt(randombetter);
}else if(i==3){
randombetter = rnd2.nextInt(d.length());
generatednumber += d.charAt(randombetter);
}
}
return generatednumber;
}
public String returnmynumber() {
return computernumber;
}
}
输出屏幕截图; game screen
}else if(result.indexOf("+")==-1 && result.indexOf("-")!=-1){
for(int i =0;i<4;i++){
if(i==0){
if(a.indexOf(Character.toString(compguess.charAt(i))) !=-1){
a.deleteCharAt(a.indexOf(Character.toString(compguess.charAt(i))));
}
}else if(i==1){
if(b.indexOf(Character.toString(compguess.charAt(i))) !=-1){
b.deleteCharAt(b.indexOf(Character.toString(compguess.charAt(i))));
}
}else if(i==2){
if(c.indexOf(Character.toString(compguess.charAt(i))) !=-1){
c.deleteCharAt(c.indexOf(Character.toString(compguess.charAt(i))));
}
}else if(i==3){
if(d.indexOf(Character.toString(compguess.charAt(i))) !=-1){
d.deleteCharAt(d.indexOf(Character.toString(compguess.charAt(i))));
}
}
}
}