在do / while循环中重置Java数组?

时间:2016-09-04 10:51:35

标签: java

对于上下文,这是一个桩游戏​​,用户选择一个数组的元素,然后拿走一个指定的数字,拿起最后一个计数器的那个获胜。所以我的问题是循环,因为当转向上升时,do / while循环中的数组会重置并重新启动。

do{
  if(userPicked == 1){
    System.out.println(player1 +" will start the game.");
    player1 = startingPlayer;
    player2 = secondPlayer;
  }else{
    System.out.println(player2+ " will start the game.");
    player2 = startingPlayer;
    player1 = secondPlayer;
  } //else
  gamesPlayed = gamesPlayed + 1; 
  System.out.println("The piles are.....");
  System.out.println("1 | " + piles [0]);
  System.out.println("2 | " + piles [1]);
  System.out.println("3 | " + piles [2]);
  System.out.println("4 | " + piles [3]);
  System.out.println("5 | " + piles [4]);


  do{  

    System.out.println(player2 + "First pick the pile you would like to minus from");
    System.out.println( "then pick the amount you would like to take away."); 
    int userPile = readInt ("What pile would you like to minus from");
    int userTokens = readInt("How many tokens would you like to take away");
    //the game goes below this 
    if(userPile == 1){
      System.out.println("1 | " + (piles [0] - userTokens));
      System.out.println("2 | " + piles [1]);
      System.out.println("3 | " + piles [2]);
      System.out.println("4 | " + piles [3]);
      System.out.println("5 | " + piles [4]);  
    }else if(userPile == 2){
      System.out.println("1 | " + piles [0]);
      System.out.println("2 | " + (piles [1] - userTokens));
      System.out.println("3 | " + piles [2]);
      System.out.println("4 | " + piles [3]);
      System.out.println("5 | " + piles [4]);  
    }else if(userPile == 3){
      System.out.println("1 | " + piles [0]);
      System.out.println("2 | " + piles [1]);
      System.out.println("3 | " + (piles [2] - userTokens));
      System.out.println("4 | " + piles [3]);
      System.out.println("5 | " + piles [4]);
    }else if(userPile == 4){
      System.out.println("1 | " + piles [0]);
      System.out.println("2 | " + piles [1]);
      System.out.println("3 | " + piles [2]);
      System.out.println("4 | " + (piles [3] - userTokens));
      System.out.println("5 | " + piles [4]);
    }else if(userPile == 5){
      System.out.println("1 | " + piles [0]);
      System.out.println("2 | " + piles [1]);
      System.out.println("3 | " + piles [2]);
      System.out.println("4 | " + piles [3]);
      System.out.println("5 | " + (piles [4] - userTokens));
    }else{
      System.out.println("An error occured"); 
    }//else

    System.out.println(player1 + "First pick the pile you would like to minus from");
    System.out.println( "then pick the amount you would like to take away."); 
    int userPile2 = readInt ("What pile would you like to minus from");
    int userTokens2 = readInt("How many tokens would you like to take away");
    //the game goes below this 
    if(userPile == 1){
      System.out.println("1 | " + (piles [0] - userTokens2));
      System.out.println("2 | " + piles [1]);
      System.out.println("3 | " + piles [2]);
      System.out.println("4 | " + piles [3]);
      System.out.println("5 | " + piles [4]);  
    }else if(userPile == 2){
      System.out.println("1 | " + piles [0]);
      System.out.println("2 | " + (piles [1] - userTokens2));
      System.out.println("3 | " + piles [2]);
      System.out.println("4 | " + piles [3]);
      System.out.println("5 | " + piles [4]);  
    }else if(userPile == 3){
      System.out.println("1 | " + piles [0]);
      System.out.println("2 | " + piles [1]);
      System.out.println("3 | " + (piles [2] - userTokens2));
      System.out.println("4 | " + piles [3]);
      System.out.println("5 | " + piles [4]);
    }else if(userPile == 4){
      System.out.println("1 | " + piles [0]);
      System.out.println("2 | " + piles [1]);
      System.out.println("3 | " + piles [2]);
      System.out.println("4 | " + (piles [3] - userTokens));
      System.out.println("1 | " + piles [4]);
    }else if(userPile == 5){
      System.out.println("1 | " + piles [0]);
          System.out.println("1 | " + piles [1]);
          System.out.println("1 | " + piles [2]);
          System.out.println("1 | " + piles [3]);
          System.out.println("5 | " + (piles [4] - userTokens));
        }else{
          System.out.println("An error occured"); 
        }//else  


      }while (piles [0] + piles [1] + piles [2] + piles [3] + piles [4] > 1 );

如果有人知道如何将已经将数字作为常数的阵列保持为常数,即使两个玩家都有一个很好的转弯。 (同样,如果我们可以将节目谈话保持在某种程度上的低位,因为我只是这样做了6周)

感谢您提前提供帮助,这让我陷入了一段时间。

1 个答案:

答案 0 :(得分:0)

由于仍然没有答案,也没有所需的反馈(complete, minimal and verifiable example),我只能建议一些编码方面的好习惯,这些方法可以帮助您避免,并最终找到问题的原因:

  • 避免使用长方法:最好在多个方法之间进行功能分配(通常是私有方法)。除此之外,这使您命名每种方法,因此,您会发现您的编码算法可读

public void extractAllData(parameters...) { if (validateParameters(...)) { a=extractPrimaryData(...); if (existSecondaryData(a)) { b=extractSecondaryData(...); } } return a+b; }

  • 尝试不重复结构:如果两个代码块非常相似,只对某些数字或表达式有所不同,最好将它们提取到 common [private] ]方法,并使差异为参数到该方法。你会节省很多时间。

  • 不要使用显式常量索引数组:a[0], a[1], a[2]等。使用变量a[i]获取accostumed to index。并且,如果要遍历数组的所有元素,则此变量必须是for循环的索引:

for (int i=0; i < array.length;i++) { doSomething(a[i]); }

这样,数组的长度应该在初始化时出现。从那时起,始终使用array.length

相关问题