gpt4 book ai didi

java - 使用数组生成两个随机值

转载 作者:行者123 更新时间:2023-12-01 13:43:55 25 4
gpt4 key购买 nike

我试图让两只宠物有两种不同的情绪状态。但是当我运行它时,它们共享相同的状态,如下所示:

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

最佳答案

这是因为您使用的是 EmotionalState 数组中的相同值。在您的方法 petInteraction() 中,您使用的 EmotionalState 索引始终为 0。它没有改变。因此,在 for 循环中获取的所有宠物最终都会根据数组第 0 个索引中存储的任何整数获得情感。您需要使用不同的索引从数组中获取不同的值。另外,根据您的代码,您使用的索引应相差 3,以使它们随时有两种不同的情绪。

编辑

在你的方法中,你有一个 for 循环,它循环两次。每次都是一个新宠物。现在,在获得宠物的名字后,您就有了一个 if-else 结构。不说您的 EmotionalState 数组在第 0 个索引中保存值 10。对于第一个宠物,它将进入第三个if。它会打印出这是危险的。在 for 循环的第二次运行中,宠物名称发生了变化,但第 0 个索引中的值仍然是 10。因此它会再次打印现在很危险。前两个 if block 永远不会输入,因为第 0 个索引中的值始终为 10 并且在程序中永远不会更改。这就是导致它打印出相同情绪状态的原因。您可以做的是,您可以在 for 循环的每次运行中生成一个随机数,并更新 EmotionalState 数组的第 0 个索引,这两个数字应该至少相差 3,以便每次运行都会进入不同的 if block 。

关于java - 使用数组生成两个随机值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20480746/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com