私は古いメイドを書こうとします。 カードを処理して仕分けした後、私はカードの2つの部分を持っています、1つはplayerDeck、もう1つはcomputerDeckです。今はペアを削除する必要があります。しかし、私はこの段階で立ち往生していました。例えば誰かがこのコード(文字列)を修正するのを助けることができます-java
(一例) playerDeck: '♡'、 '♢'、 '8♡'、 '8♢'、 '8♠'、 'Q♠'、 '2♠'、 '4♣'、 '7♢'、 '7♣'、 'K♣'、 'A♡'、 'J♡'、 '9♣'、 '3♢'
computerDeck: '3♡' 「10♠」、「10♣」、「6♡」、「K♡」、「K♢」、「A♣」、「A♠」、「4♢」、「3♣」、「10♡」、 、 '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♣'、 'ペアを削除したときに2つの空白があるので、3♢ ' computerDeck:'10♣'、 '6♡'、 '4♢'
またはこれを行う別の方法があります。 1文字目を比較するために
if(q.equals(p)){//q==p if true,they are save in the same location-this may not be what you want,and in this code it will be false forever.
}
あなたはどんな出力を期待していますか、どんな出力を得ていますか? –
playerDeck:「8♠」「Q♠」「2♠」「4♣」「K♣」「A♡」「J♡」「9♣」「3♢」 computerDeck: '10♣ '、' 6♡ '、' 4♢ ' –
長い行を短い行に分割します。非常に長い行のコードを読むのは苦痛です。なぜなら、行の終わりを見るためにスクロールすると、残りのプログラムが見えなくなるからです。一般的なリミーンは "80文字より長い行はありません" – tucuxi