【问题标题】:can someone help me fix this code(string)-java有人可以帮我修复这个代码(字符串)-java
【发布时间】: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♠','1​​0♣','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


【解决方案1】:

要比较第一个字符,请更改此行

 if (q == p) {

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

注意q == p 会检查qp 是否引用了相同的字符串,根本不看内容。如果您想按内容比较完整字符串(或任何其他不是 char、int 等的对象),您应该使用 equalsq.equals(p) 仅在两者具有相同内容时才返回 true。

【讨论】:

  • 现在它在 test.main(test.java.52) 的第 52 行表示“线程“main”java.lang.NullPointerException 中的异常是“p=p.substring(0,1); "怎么了
  • p 在这一点上是 nullnull 引用不指向实际对象;并且尝试在空引用上调用substring(0,1) 是错误的(因为没有实际对象来接收调用)。至于 why p 为 null,您可能忘记初始化它的值。例如,当将 String 数组声明为 String a[] = new String[10] 时,数组中的所有 10 个 String 引用开始指向 null
【解决方案2】:

如果要比较两个字符串,可以使用'equals',like

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.

}

【讨论】:

  • 我只想比较第一个字符
  • 字符串不是原始类型,不像char,只能用equals来测试字符串是否相等。
  • @Joe 用 toCharArray 转换,然后比较每个新数组的第一个元素
  • @MarkYisri 为什么要分配一个新的字符数组来检查第一个元素?
  • @Silverclaw 虽然我知道这是一种浪费,但它允许以后编写更灵活的代码。当然,OP 总是可以使用this
猜你喜欢
  • 1970-01-01
  • 2017-05-26
  • 2020-03-25
  • 1970-01-01
  • 2017-02-17
  • 2023-03-06
  • 1970-01-01
  • 2013-04-01
  • 2016-03-08
相关资源
最近更新 更多