【发布时间】:2017-01-24 06:50:12
【问题描述】:
我试着写一个老处女。 发牌和排序后,我有两部分牌,一个是 playerDeck,一个是 computerDeck。现在需要移除这些对。但我被困在这个阶段。
例如(只是一个例子) 玩家甲板: 'A♡','A♢','8♡','8♢','8♠','Q♠','2♠','4♣','7♢','7♣', 'K♣'、'A♡'、'J♡'、'9♣'、'3♢'
电脑甲板: '3♡','3♣','10♡','10♠','10♣','6♡','K♡','K♢','A♣','A♠', '4♢'、'7♡'、'7♠'
String q;
String p;
ArrayStringsTools AA=new ArrayStringsTools();//this is a class that i will use for removing item
for(int i=0;i<playerDeck.length-1;i++){
q=playerDeck[i];
q=q.substring(0,1);//i try to find the first character
p=playerDeck[i+1];//after finding first character, i can compare them,and if they are same, then i can remove them
p=p.substring(0,1);
if(q==p){
AA.removeItemByIndex(playerDeck,26,i);//this is the method that i used for removing same item,i will put this code below
AA.removeItemByIndex(playerDeck,26,i+1);//there are 51 cards in total,player has 26, computer has 25
}
}
public static int removeItemByIndex(String[] arrayOfStrings, int currentSize, int itemToRemove){//this is the method i used for removing item(first is the array of Deck, second is the size of Deck,third is the index of item to remove)
if( arrayOfStrings == null || currentSize > arrayOfStrings.length) {
System.out.println("ArrayStringsTools.removeItemByIndex: wrong call");
return currentSize;
}
if( itemToRemove < 0 || itemToRemove >= currentSize ) {
System.out.println("ArrayStringsTools.removeItem: item "
+ itemToRemove + " out of bounds. Array Unchanged.");
return currentSize;
}
int i;
for( i = itemToRemove; i < currentSize-1; i++){
arrayOfStrings[i] = arrayOfStrings[i+1];
}
arrayOfStrings[i]= null;
return currentSize-1;
我认为我写得对,但它与原产地相比没有任何区别。 结果应该是: playerDeck:'8♠','Q♠','2♠','4♣','K♣','A♡','J♡','9♣','3♢' computerDeck:'10♣', '6♡', '4♢'
或者有没有别的办法,因为拆掉一对的时候,有两个空位,所以……苦苦挣扎了好久……
【问题讨论】:
-
你期待什么输出,你得到什么输出??
-
playerDeck:'8♠','Q♠','2♠','4♣','K♣','A♡','J♡','9♣', '3♢' computerDeck:'10♣', '6♡', '4♢'
-
把你的长线分成更短的线。阅读很长的代码行很痛苦,因为如果您滚动查看行尾,您会看不到程序的其余部分。一个常见的 limin 是“不超过 80 个字符的行”
标签: java