Java 对数组排序并将结果用于if语句

Java 对数组排序并将结果用于if语句,java,arrays,sorting,Java,Arrays,Sorting,程序首先要求用户输入两个宠物名。然后,它将打印他们的愤怒程度,如下所示: 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(…)的方法中完成了这项工作,这就是我遇到的问题 我希望它执行代码的喂食/歌唱部分,首先在愤怒的

程序首先要求用户输入两个宠物名。然后,它将打印他们的愤怒程度,如下所示:

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,如果否,则像往常一样运行代码

以下是我正在使用的完整代码:

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
import javax.swing.*;
导入java.util.array;
导入java.util.Random;
公共类alientpetprogram
{
公共静态void main(字符串[]args)
{   
//为宠物的情绪状态生成1-10之间的随机值
int[]状态=新int[3];
随机情绪=新随机();

对于(int i=0;i您需要将状态与宠物名称相关联。我建议您创建一个带有名称及其情绪状态的类
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();
   }
}
然后按已执行的操作对阵列进行排序:

Arrays.sort(pets);

现在数组中的第一个对象将是最愤怒的宠物,您可以使用适当的类getter方法轻松提取它的名字。

好的,我更改了一些内容,从这里开始,我希望您知道它应该朝哪个方向发展。:) 现在仍然存在错误,您必须修复它们。如果您有两个宠物,为什么要处理3种情绪状态?在创建宠物并为其提供情绪状态后,您不应该再使用情绪状态(java约定的首字母小写)数组

只是一些附加信息:Java是一种
面向对象编程
语言,因为您可以将真实对象表示为类(因此可以通过
new
创建instances=Object)。这就是为什么应该使用附加类
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;
}
 }

使用
String carePet=JOptionPane.showInputDialog(“您想先照顾愤怒的宠物吗?”);if(carePet.equalsIgnoreCase(“是”){}else if(carePet.equalsIgnoreCase(“否”){System.out.println(“+EmotionalState[SortAnger]”);
我目前使用的格式,因为我不知道如何在我的代码中实现你的建议。现在不可能,因为宠物一创建,感觉部分就会打印出来,你会在之后进行排序。因此,首先用户需要创建宠物,然后你可以询问他是否希望最愤怒的宠物打印feeling语句。Giorashc设计提示让你的生活更轻松,我也建议你,因为现在你的宠物和情感是松散耦合的。你知道我的意思吗?:)我的回答对你有帮助吗?如果你有任何问题,请随时提问!
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;
}
 }
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