该程序首先要求用户输入两个宠物名称。然后它会打印他们的愤怒程度如下:对数组进行排序并将结果用于if语句
Oh cool, Poodle is a very unique name!
Poodle is feeling tetchy!
Oh cool, Ollie is a very unique name!
Ollie is DANGEROUS RIGHT NOW!!!.
然后它会问你是否想先照顾愤怒的宠物。我已经在称为sort(...)的方法中完成了这个工作,而这正是我卡住的地方。
我希望它执行代码的喂食/唱歌部分,它发生在愤怒宠物的petInteraction(...)中,如果用户在被要求时输入Yes,如果没有,则运行代码照常。
下面是完整的代码IM与合作:
import javax.swing.*;
import java.util.Arrays;
import java.util.Random;
public class alientpetprogram
{
public static void main(String[] args)
{
//Generates a random value from 1-10 for the pet's emotional state
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++) //A for loop is used to store two pet's in an array. An emotional state is then assigned to the pet.
{
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!!!.");
}
EmotionalState[0] = (int)(Math.random()*0+9);
}
sort(EmotionalState, petName);
return petName;
} //ENDS petInteraction
public static void sort(int[] EmotionalState, String [] petName)
{
Arrays.sort(EmotionalState);
for (int SortAnger = 0; SortAnger < EmotionalState.length; SortAnger++)
{
String carePet = JOptionPane.showInputDialog("Would you like to take care of the angrier pet first?");
if(carePet.equalsIgnoreCase("Yes"))
{
}
else if(carePet.equalsIgnoreCase("No"))
{
}
System.out.println(""+ EmotionalState[SortAnger]);
}
}
public static void petEmotion(int[] EmotionalState, String [] petName)
{
String[] petsName = petInteraction(EmotionalState, petName);
String userinput;
userinput=JOptionPane.showInputDialog("choose how many rounds?"); //Allows the user to set the 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]);
EmotionalState[0] = (int)(Math.random()*0+9);
EmotionalState[1] = (int)(Math.random()*0+9);
EmotionalState[2] = (int)(Math.random()*0+9);
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]);
//The for loop below is used to reduce the Thirst, Hunger and Irritability levels for pet one and two
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
难道我的回答帮助你的?如果您有任何问题,请随时询问! – Ben