我试图让两只宠物有两种不同的情绪状态。但是当我运行它时,它们共享相同的状态,如下所示:使用数组产生两个随机值
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
我强烈建议您学习面向这种涉及状态的项目类型的面向对象编程FAST。你可以简单地为一个宠物对象创建一个基础构造器,并且初始的“状态”如'hungerLVL','thirstLVL','irritiabilityLVL'等等。你的构造函数允许你随意改变这些值。 ''新宠物(5,6,10)'分别为'LVL's。您的方法可以检索或更改这些状态。不要为此做程序,因为它会使编程变得困难,并且使得将来的维护令人难以置信地变得烦人。 – theGreenCabbage