簡體   English   中英

使用數組生成兩個隨機值

[英]Using an array to generate two random values

我試圖讓兩只寵物具有兩種不同的情緒狀態。 但是當我運行它時,它們共享相同的狀態,如下所示:

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循環,以便每個pet都有不同的值,但是我不確定我是否正確執行了操作。

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

這是整個代碼。

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

那是因為您正在使用EmotionalState數組中的相同值。 在您的方法petInteraction() ,您使用的EmotionalState索引始終為0。 它沒有改變。 因此,根據在數組第0個索引中存儲的任何整數,在for循環中獲取的所有pet最終都會產生情緒。 您需要使用不同的索引來從數組中獲取不同的值。 同樣,根據您的代碼,您使用的索引應該相差3,以使它們隨時具有兩種不同的情感。

編輯

在您的方法中,您有一個for循環,該循環循環兩次。每次創建一只新寵物。 現在,您獲得了寵物的名字之后,便有了一個if-else構造。 不說您的EmotionalState數組在第0個索引中的值為10。 對於第一只寵物,它將進入第三只寵物。 它會打印出危險。 在for循環的第二次運行中,寵物名稱更改,但第0個索引中的值仍為10。因此它將再次打印出現在很危險的信息。 永遠不會輸入前兩個if塊,因為第0個索引中的值始終為10,並且在程序中永不更改。 這就是導致它表現出相同情緒狀態的原因。 您可以做的是,可以在for循環的每次運行中生成一個隨機數,並更新EmotionalState數組的第0個索引,這兩個數字應至少相差3,以便每次運行時都輸入不同的if塊。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM