2013-12-09 55 views
0

我試圖讓兩隻寵物有兩種不同的情緒狀態。但是當我運行它時,它們共享相同的狀態,如下所示:使用數組產生兩個隨機值

Oh cool, asda is a very unique name! 
asda is DANGEROUS RIGHT NOW!!!. 
Oh cool, polo is a very unique name! 
polo is DANGEROUS RIGHT NOW!!!. 

asda's irrtability level is 4 
asda's thirst level is 8 
asda's hunger level is 5 

polo's irrtability level is 4 
polo's thirst level is 8 
polo's hunger level is 5 

我已經將情緒狀態值存儲在數組中。

我試過使用for循環,以便每個寵物會有不同的值,但我不知道如果我做得正確。

所以我應該爲情緒狀態創建兩個不同的數組,還是有更好的做事方式?

這裏是整個代碼。

import javax.swing.*; 
import java.util.Random; 
public class alientpetprogram 
{ 

public static void main(String[] args) 
{ 

    int[] EmotionalState = new int [3]; 
    Random emotion = new Random(); 
    for(int i = 0; i <= 2; i++) 
    { 
    int hungerLVL = emotion.nextInt(10) + 1; 
    EmotionalState[0] = hungerLVL; 

    int thirstLVL = emotion.nextInt(10) + 1; 
    EmotionalState[1] = thirstLVL; 

    int irritabilityLVL = emotion.nextInt(10) + 1; 
    EmotionalState[2] = irritabilityLVL; 
    } 

    String [] petName = new String [2]; 
    petEmotion(EmotionalState, petName); 

    System.exit(0); 
} //ENDS main 

    public static String[] petInteraction(int[] EmotionalState, String [] petName) //Use this further on in petEmotion() 
    { 
     for(int i = 0; i < 2; i++) 
     { 
      petName[i] = JOptionPane.showInputDialog("What is your pet called?"); 
      System.out.println("Oh cool, " + petName[i] + " is a very unique name!"); 


      if (EmotionalState[0] == 1 || EmotionalState[0] == 2 || EmotionalState[0] == 3) 
      { 
       System.out.println(petName[i] + " is feeling Calm."); 
      } 
      else if (EmotionalState[0] == 4 || EmotionalState[0] == 5 || EmotionalState[0] == 6) 
      { 
       System.out.println(petName[i] + " is feeling tetchy!"); 
      } 
      else if (EmotionalState[0] == 7 || EmotionalState[0] == 8 || EmotionalState[0] == 9 || EmotionalState[0] == 10) 
      { 
       System.out.println(petName[i] + " is DANGEROUS RIGHT NOW!!!."); 
      } 

     } 
     return petName; 
    } //ENDS petInteraction 

       public static void petEmotion(int[] EmotionalState, String [] petName) //This method changes the emotional states of the pet. 
       { 
         String[] petsName = petInteraction(EmotionalState, petName); 

         String userinput; 
         userinput=JOptionPane.showInputDialog("choose how many rounds?"); 
         int roundsuggestion=Integer.parseInt(userinput); 



         for (int round =1; round <=roundsuggestion; round++) //sets the amount of rounds the game runs for.   
         { 
         System.out.println("Round " + roundsuggestion);     
         System.out.println(petsName[0] + "'s irrtability level is " + EmotionalState[2]); 
         System.out.println(petsName[0] + "'s thirst level is " + EmotionalState[1]); 
         System.out.println(petsName[0] + "'s hunger level is " + EmotionalState[0]); 
         System.out.println(petsName[1] + "'s irrtability level is " + EmotionalState[2]); 
         System.out.println(petsName[1] + "'s thirst level is " + EmotionalState[1]); 
         System.out.println(petsName[1] + "'s hunger level is " + EmotionalState[0]); 


         for(int y=1; y<=2; y++) 
         { 
          String askToReduceIrritable = JOptionPane.showInputDialog("Would you like to sing for " + petsName[0] + " in order to lower the pets irritability level?"); 

          if (askToReduceIrritable.equalsIgnoreCase("Yes")) 
          { 
           EmotionalState[2] = EmotionalState[2] - 1; 
           System.out.println(petsName[0] + "'s irrtability level is now " + EmotionalState[2]);       
          } 

          String askToReduceThirst = JOptionPane.showInputDialog("Would you like to give " + petsName[0] + " some water in order to reduce the thirst level?"); 

          if (askToReduceThirst.equalsIgnoreCase("Yes")) 
          { 
           EmotionalState[1] = EmotionalState[1] - 1; 
           System.out.println(petsName[0] + "'s thirst level is now " + EmotionalState[1]);       
          } 

          String askToReduceHunger = JOptionPane.showInputDialog("Would you like to give " + petsName[0] + " some food in order to reduce the hunger level?"); 

          if (askToReduceHunger.equalsIgnoreCase("Yes")) 
          { 
           EmotionalState[0] = EmotionalState[0] - 1; 
           System.out.println(petsName[0] + "'s hunger level is now " + EmotionalState[0]);       
          }   
          System.out.println(""); 
          System.out.println("You will now take care of the second pet"); 

          String askToReduceIrritableTwo = JOptionPane.showInputDialog("Would you like to sing for " + petsName[1] + " in order to lower the pets irritability level?"); 

          if (askToReduceIrritableTwo.equalsIgnoreCase("Yes")) 
          { 
           EmotionalState[2] = EmotionalState[2] - 1; 
           System.out.println(petsName[1] + "'s irrtability level is now " + EmotionalState[2]);       
          } 

          String askToReduceThirstTwo = JOptionPane.showInputDialog("Would you like to give " + petsName[1] + " some water in order to reduce the thirst level?"); 

          if (askToReduceThirstTwo.equalsIgnoreCase("Yes")) 
          { 
           EmotionalState[1] = EmotionalState[1] - 1; 
           System.out.println(petsName[1] + "'s thirst level is now " + EmotionalState[1]);       
          } 

          String askToReduceHungerTwo = JOptionPane.showInputDialog("Would you like to give " + petsName[1] + " some food in order to reduce the hunger level?"); 

          if (askToReduceHungerTwo.equalsIgnoreCase("Yes")) 
          { 
           EmotionalState[0] = EmotionalState[0] - 1; 
           System.out.println(petsName[1] + "'s hunger level is now " + EmotionalState[0]);       
          }   
          String exitGame = JOptionPane.showInputDialog("Would you like to exit the game? Type yes/no"); 

          if (exitGame.equalsIgnoreCase("Yes")) 
          { 
           System.exit(0);      
          } 
          else 
          System.out.println(""); 
          JOptionPane.showMessageDialog(null, "A new round has begun!"); 
         } // END second loop 
      } // END first loop 
       }//ENDS petEmotion     

} //ENDS Class alientpetprogram 
+0

我強烈建議您學習面向這種涉及狀態的項目類型的面向對象編程FAST。你可以簡單地爲一個寵物對象創建一個基礎構造器,並且初始的「狀態」如'hungerLVL','thirstLVL','irritiabilityLVL'等等。你的構造函數允許你隨意改變這些值。 ''新寵物(5,6,10)'分別爲'LVL's。您的方法可以檢索或更改這些狀態​​。不要爲此做程序,因爲它會使編程變得困難,並且使得將來的維護令人難以置信地變得煩人。 – theGreenCabbage

回答

1

這是因爲您正在使用EmotionalState數組中的相同值。在您的方法petInteraction()中,您正在使用的EmotionalState索引始終爲0。它沒有改變。因此,在for循環中獲取的所有寵物將根據存儲在數組的第0個索引中的任何整數結束感情。您需要使用不同的索引來從數組中獲取不同的值。此外,根據你的代碼,你使用的指數應該有3的差異,讓他們在任何時候都有兩種不同的情緒。

編輯

在你的方法,你有一個for循環,其循環2個times.Each時間爲一個新的寵物。現在獲得寵物的名字後,你有一個if-else結構。不要說你的EmotionalState數組在第0個索引中保持值10。對於第一隻寵物來說,它會進入第三隻寵物。它會打印出它很危險。在for循環的第二次運行中,寵物名稱會更改,但第0個索引中的值仍然爲10.因此,它會再次打印出現在是危險的。前兩個if塊永遠不會進入,因爲第0個索引中的值始終爲10,並且在程序中永遠不會更改。那是什麼導致它打印出同樣的情緒狀態。你可以做的是,你可以在for循環的每次運行中生成一個隨機數並更新EmotionalState數組的第0個索引,這兩個數字應該有至少3的差值,這樣每次運行它都會進入一個不同的if塊。

+0

我已經使用情緒狀態[0],因爲它擁有飢餓感。取決於關卡,我想打印相應的信息。我無法想到另一種方式。 我不明白你的意思是有什麼區別3. – AbbenGabben

+0

@AlexiaBatyn我已經在我的答案的編輯部分解釋它。 – Adarsh

+0

對不起,我誤解了評論。我編輯了我的答案。 – Adarsh