在我的循环

时间:2015-11-29 09:39:07

标签: java arrays

目前我的代码有点问题,这给我带来了很大的压力。基本上我正在尝试编写一个棋盘游戏,以圆形格式询问玩家问题,从玩家1切换到玩家2并将他们的得分/棋盘位置保持在阵列中。我似乎无法弄清楚如何做到这一点,但目前还有一个更紧迫的问题。

目前我感到困惑的是,为什么我在不同方法中创建的数组被完全忽略了控制一堆问题的循环。我很确定我已经正确地将数组方法传递给questions方法,因为我在循环中放入了一行代码,以便在每个问题之后打印出数组,它会给我一块板位置(虽然这里有一个问题,但它没有累积更新。也就是说,在第一个问题之后我会在第3个位置但是在第二个问题之后它会告诉我向前移动5个位置到第5个位置,而不是8)。当我告诉代码打印循环的外部时,它只打印出0,这正是我不希望它做的。

public static void main (String[] param)
{ 
   numberPlayers();
   yourQuiz();
   int[] scoretrack = scorearray();
   int diceroll = dicethrow(6);
   questions(diceroll, scoretrack);
   System.exit(0);
}  


public static int[] scorearray()
{
   int scoreplayer1 = 0;
   int scoreplayer2 = 0;
   int[] scoretrack = {scoreplayer1, scoreplayer2};
   return  scoretrack;
}

/********************************************************************************************************
This method is where the questions are asked to the players. A dice is rolled to determine what question
is asked to the player and then, depending on if the answer is correct, another dice is rolled to see how
many squares the player advances. The square that the player is on is stored in score[0] for player 1 and score[1]
for player 2. This is where the main problem lies; score[0] doesn't store the board place cumulatively, 
i.e when the second question is asked the boardplace from the first question isn't stored
*********************************************************************************************************/

public static int questions(int diceroll, int[] score)
{
String playertwo;
int ans1 = 0;
String textinput;
int result;
String continueornot = input("Do you wish to continue?");
scorearray();

 while (!continueornot.equalsIgnoreCase("No"))
 {

  if (diceroll == 1)
  { 

   while (ans1 != 19)
   {
      textinput = input("What's 9+10?");
      ans1 = Integer.parseInt(textinput);
      output("That's certainly an interesting answer"); 

    if (ans1 == 19)
    {
       output("FANTASTIC ANSWER, THAT'S CORRECT!");
       diceroll = dicethrow(diceroll);
       score[0] = score[0] + diceroll;
       output("Move forward " + diceroll + " squares. You are on square " + score[0]);
       System.out.println(score[0]); // this prints out the score here, but not cumulatively
       return yourQuiz();
    } // if they get the answer right

    else 
    {
       playertwo = input("Sorry, the answer was 19. Next player type yes to continue or no to exit");

        if (playertwo.equalsIgnoreCase("yes"))
        {
            //return questions(diceroll, score);
        }

        else 
        {
           System.out.println(score[0]);
           System.exit(0);
        }
    } // if they get the answer wrong

   } // END while 1

  } // END if dice rolls 1

} // end while answer isn't no

   System.out.println(score[0]); // this doesn't actually print out what the boardplace is, it just prints 0 twice. Array is being ignored?

   return 500; // had to return a random number because it was giving me a 'return value missing' error

如果对我要问的内容或代码中的内容有什么不清楚的地方,请问我,我会尝试澄清。我真的非常渴望得到帮助,如果有人能够指出我已经搞砸了什么,我会永远感激,因为我现在已经非常非常长时间地讨论了这个问题。谢谢!

0 个答案:

没有答案