2017-01-24 7 views
1

私は古いメイドを書こうとします。 カードを処理して仕分けした後、私はカードの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. 

} 
+0

あなたはどんな出力を期待していますか、どんな出力を得ていますか? –

+0

playerDeck:「8♠」「Q♠」「2♠」「4♣」「K♣」「A♡」「J♡」「9♣」「3♢」 computerDeck: '10♣ '、' 6♡ '、' 4♢ ' –

+0

長い行を短い行に分割します。非常に長い行のコードを読むのは苦痛です。なぜなら、行の終わりを見るためにスクロールすると、残りのプログラムが見えなくなるからです。一般的なリミーンは "80文字より長い行はありません" – tucuxi

答えて

0

のように、「等しく「あなたは2つの文字列を比較したい場合は、使用することができます

+0

これはtest.main(test.java.52)のmain 'java.lang.NullPointerException'の'exception in thread 'です。52行目は「p = p.substring(0,1);」です。何が間違っています –

+0

'p'はその時点で' null'です。ヌル参照は実際のオブジェクトを指しません。実際のオブジェクトを呼び出すことがないため、null参照で 'substring(0,1)'を呼び出すとエラーになります。 *なぜ* pがnullであるかに関しては、おそらくあなたはその値を初期化するのを忘れていたでしょう。たとえば 'String'の配列を' String a [] = new String [10] 'と宣言すると、配列中の10個の' String'参照はすべて 'null'を指し始めます。 – tucuxi

0

......長い間苦労されてしまって、この行を変更

チェックが qp同じ文字列を参照してくださいかどうかを確認するには、すべての内容を見ていないことを
if (q == p) { 

if (q.charAt(0) == p.charAt(0)) { 

に注意してください。完全な文字列(またはchar、intなどでない他のオブジェクト)をコンテンツで比較する場合は、 equalsを使用する必要があります。 q.equals(p)は、両方が同じ内容の場合にのみtrueを返します。

+0

私はちょうど最初の文字を比較したい –

+0

文字列はcharと同じではなく、プリミティブ型ではなく、equalsを使用して等価性をテストすることができます。 –

+2

@JoeはtoCharArrayで変換してから、新しい各配列の最初の要素を比較します。 –

関連する問題