2013-12-10 64 views
0

该程序首先要求用户输入两个宠物名称。然后它会打印他们的愤怒程度如下:对数组进行排序并将结果用于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 
+0

难道我的回答帮助你的?如果您有任何问题,请随时询问! – Ben

回答

0

您需要的状态与宠物相关name.I'll建议你创建一个类Pet的名称和它的情绪状态,它实现Comparable接口,所以你可以根据他们的愤怒状态轻松地对宠物进行排序。

public class Pet implements Comparable<Pet> { 
    private String name; 
    private int state; 

    // Add required basic methods here 

    int compareTo(Pet pet) { 
     return state - pet.getState(); 
    } 
} 

然后定义数组:

Pet pets[] = // Initialize with Pet objects 

数组然后进行排序,你已经做了:

Arrays.sort(pets); 

现在数组中的第一个对象将是angriest宠物和你可以用适当的类getter方法轻松地提取他的名字。

+0

是不是可以通过使用'String carePet = JOptionPane.showInputDialog(“你想先照顾愤怒的宠物?”)来实现我所需要的东西? (carePet.equalsIgnoreCase(“Yes”)) { } else if(carePet。equalsIgnoreCase( “否”)){ } 的System.out.println( “” + EmotionalState [SortAnger]);' 格式,我现在有,因为我不知道我将如何实现在你的建议我的代码。 – AbbenGabben

+1

现在它不可能,因为一旦宠物被创建,感觉部分就会被打印出来,之后你就进行排序。因此,首先用户需要制作宠物,然后你可以问他是否想让愤怒的人打印这些感觉陈述。 Giorashc的设计提示让你的生活变得更轻松,我也建议你现在养宠物和情绪松散结合。你懂我的意思吗? :) – Ben

0

好吧我改变了一些东西,从这里开始,我希望你知道它应该采取哪个方向。 :) 现在仍然存在错误,您必须修复它们。如果你有两只宠物,你为什么在3种情绪状态下工作?在创建宠物并给予他们情绪状态后,你不应该再使用emotionState(java convention的第一个字母小写)数组。

只是一些附加信息:Java是Object-Oriented Programming语言,因为您可以将实际对象表示为类(因此可通过new创建实例=对象)。这就是为什么应该使用额外的类Pet,因为你代表了一个真实的世界。 :)(ofc不仅仅是我们世界中真正存在的东西,外星人也被允许:P)

以下是我到目前为止改变的代码,正如开始时提到的,您必须修改整个代码,因为有些东西不是兼容了。

希望这会有所帮助!

Pet

public class Pet implements Comparable<Pet> { 
private String name; 
private int state; 

/** 
* @return Returns the state. 
*/ 
public int getState() 
{ 
    return state; 
} 

/** 
* @param state The state to set. 
*/ 
public void setState(int state) 
{ 
    this.state = state; 
} 

public int compareTo(Pet pet) { 
    return state - pet.getState(); 
} 

/** 
* @return Returns the name. 
*/ 
public String getName() 
{ 
    return name; 
} 

/** 
* @param name The name to set. 
*/ 
public void setName(String name) 
{ 
    this.name = name; 
} 
} 

alientpetprogram

import java.util.Arrays; 
import java.util.Random; 

import javax.swing.JOptionPane; 

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; 
    } 

    //fill the array already with 2 pets 
    Pet pets[] = new Pet[]{new Pet(), new Pet()}; 

    petEmotion(emotionalState, pets);  
    System.exit(0); 
} //ENDS main 

    public static Pet[] petInteraction(int[] emotionalState, Pet[] pets) //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. 
     { 
      String tempPetName = JOptionPane.showInputDialog("What is your pet called?"); 
      pets[i].setName(tempPetName); 
      pets[i].setState(emotionalState[i]); 
      System.out.println("Oh cool, " + pets[i].getName() + " is a very unique name!"); 
     } 

     Arrays.sort(pets); 

     String carePet = JOptionPane.showInputDialog("Would you like to take care of the angrier pet first?"); 
     if(carePet.equalsIgnoreCase("Yes")) 
     { 
      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. 
      { 
       if (emotionalState[0] == 1 || emotionalState[0] == 2 || emotionalState[0] == 3) 
       { 
        System.out.println(pets[i] + " is feeling Calm."); 
       } 
       else if (emotionalState[0] == 4 || emotionalState[0] == 5 || emotionalState[0] == 6) 
       { 
        System.out.println(pets[i] + " is feeling tetchy!"); 
       } 
       else if (emotionalState[0] == 7 || emotionalState[0] == 8 || emotionalState[0] == 9 || emotionalState[0] == 10) 
       { 
        System.out.println(pets[i] + " is DANGEROUS RIGHT NOW!!!."); 
       } 
        emotionalState[0] = (int)(Math.random()*0+9); 
      } 
     } 
     else if(carePet.equalsIgnoreCase("No")) 
     { 
      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. 
      { 
       if (emotionalState[0] == 1 || emotionalState[0] == 2 || emotionalState[0] == 3) 
       { 
        System.out.println(pets[i] + " is feeling Calm."); 
       } 
       else if (emotionalState[0] == 4 || emotionalState[0] == 5 || emotionalState[0] == 6) 
       { 
        System.out.println(pets[i] + " is feeling tetchy!"); 
       } 
       else if (emotionalState[0] == 7 || emotionalState[0] == 8 || emotionalState[0] == 9 || emotionalState[0] == 10) 
       { 
        System.out.println(pets[i] + " is DANGEROUS RIGHT NOW!!!."); 
       } 
        emotionalState[0] = (int)(Math.random()*0+9); 
      } 
     } 

     return pets; 
    } //ENDS petInteraction 


       public static void petEmotion(int[] emotionalState, Pet[] pets) 
       { 
        pets = petInteraction(emotionalState, pets); 

        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(pets[0].getName() + "'s irrtability level is " + emotionalState[2]); 
         System.out.println(pets[0].getName() + "'s thirst level is " + emotionalState[1]); 
         System.out.println(pets[0].getName() + "'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(pets[1].getName() + "'s irrtability level is " + emotionalState[2]); 
         System.out.println(pets[1].getName() + "'s thirst level is " + emotionalState[1]); 
         System.out.println(pets[1].getName() + "'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 " + pets[1].getName() + " in order to lower the pets irritability level?"); 

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

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

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

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

          if (askToReduceHunger.equalsIgnoreCase("Yes")) 
          { 
           emotionalState[0] = emotionalState[0] - 1; 
           System.out.println(pets[0].getName() + "'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 " + pets[1].getName() + " in order to lower the pets irritability level?"); 

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

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

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

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

          if (askToReduceHungerTwo.equalsIgnoreCase("Yes")) 
          { 
           emotionalState[0] = emotionalState[0] - 1; 
           System.out.println(pets[1].getName() + "'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