简体   繁体   English

如何从对象列表中找到最小的变量? (JAVA)

[英]How do I find the smallest variable from a list of objects? (JAVA)

The code doesn't show any errors. 该代码未显示任何错误。 However, I am getting an unexpected output. 但是,我得到了意外的输出。 I am supposed to get the smallest number for age, but what I keep getting is the last value entered for age. 我应该获得年龄最小的数字,但是我不断得到的是年龄输入的最后一个值。 Can you help me point out the mistakes in this code? 您能帮我指出这段代码中的错误吗?

Maybe there is some logical error in the getYoungestPet() method? 也许getYoungestPet()方法中存在一些逻辑错误?

package pet;

public class Pet 
{
    public static String petName; 
    public static int petAge, petWeight;
    int youngestAge=9999;
    static int test;




    public static String setPetName()
    {
      return petName;
    }

    public int setPetAge()
    {

        return petAge;
    }

    public int setPetWeight()
    {

        return petWeight;
    }


    public int getYoungestPet() //probably an error here..?
    {

        if (petAge<youngestAge)
            youngestAge=petAge;
        return youngestAge;

    }



}

package pet;
import java.util.Scanner;
public class PetMain extends Pet
{


    public static void main(String[] args)
    {

    System.out.println("How many pets do you want to enter? " ); 
    Scanner data= new Scanner(System.in);

    int petNumber=data.nextInt();

    for (int i = 1;i<=petNumber; i++)
    {
    Pet PetObject = new Pet(); 

    System.out.println("Please enter name for Pet " + i );  
     Scanner input = new Scanner(System.in);
       petName= input.next();        
    System.out.println("Your pet's name is : " + petName);
    System.out.println(" ");

    System.out.println("Please enter " + petName + "'s Age" );
       petAge= input.nextInt();    
    System.out.println("Your pet's age is : " + petAge);
    System.out.println(" ");

    System.out.println("Please enter " + petName + "'s Weight" );
        petWeight= input.nextInt();
    System.out.println("Your pet's weight is : " + petWeight);
    System.out.println(" ");                

        System.out.println(PetObject.getYoungestPet());

    }


}
}

The code is supposed to show the smallest age but it shows the latest entered age. 该代码应显示最小的年龄,但显示最新输入的年龄。

you should declare youngestAge as static variable. 您应该将youngestAge声明为静态变量。 so that all of the petObject could share the same value. 这样所有的petObject可以共享相同的值。

static int youngestAge=9999;

your setter and getter methods are also not proper. 您的setter和getter方法也不正确。

public static String setPetName()
{
  return petName;
}

should be: 应该:

public static void setPetName(String name)
{
  petName=name;
}

Also don't forget to set values into PetObject from main method. 同样不要忘记从main方法将值设置到PetObject

...
petName= input.next(); 
PetObject.setPetName(petName);
...

Each time you are creating a Pet , you are getting a different youngestAge with value 9999 for that object. 每次创建Pet时 ,都会为该对象获得一个不同的youngestAge值9999。 So each time it is comparing the latest petAge with 9999 and giving you the latest petAge as your enterd petAge is less than 9999. 因此,每次将最新的petAge与9999进行比较,并为您提供最新的petAge,因为您输入的petAge小于9999。

If you need to store the smallest age, then keep it in a static field. 如果需要存储最小年龄,则将其保留在静态字段中。 Cause, keeping an extra field to store the smallest age for all object is redundant for memory. 原因是,保留一个额外的字段来存储所有对象的最小寿命对于内存来说是多余的。

If you want your desired output with the existing design, then do this: 如果要在现有设计中获得所需的输出,请执行以下操作:

Make youngestAge static: 使年龄最小化

static int youngestAge=9999;

And also don't forget to make the method static too. 并且也不要忘记使方法静态化。 There is no need to make it object property anymore, both the field variables, it is using, are static. 不再需要使它成为对象属性,它正在使用的两个字段变量都是静态的。

public static int getYoungestPet()
{
    if (petAge<youngestAge)
        youngestAge=petAge;
    return youngestAge;
}

There are many things that are problematic with this code. 此代码有很多问题。

But just to answer your question directly, think about how many pet objects there could be in this program if every time the for loop runs it recreates the pet object because it is inside the for loop. 但是只是直接回答您的问题,请考虑一下,如果每次运行for循环时,它都在for循环内重新创建pet对象,则该程序中可能有多少个pet对象。 however, simply moving it outside the for loop will not help because then you will simply keep resetting the values of the same pet object every time you run the for loop. 但是,将其简单地移到for循环之外将无济于事,因为每次运行for循环时,您只需保持重置同一pet对象的值即可。 Consider making an array of pet objects. 考虑制作一系列宠物物品。

Also, your code never actually accesses the pet objects instance variables 另外,您的代码从不实际访问pet对象实例变量

In addition, there are other problems with your use of static as others have pointed out. 另外,正如其他人指出的那样,您使用static时还存在其他问题。 cheers. 干杯。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何从对象列表中提取K个“最小”元素? - How do I extract the K “smallest” elements from a list of objects? 如何在列表中找到最小值<e> ?</e> - How do I find the smallest value in a List<E>? 如何在文本文件中找到最小的数字? (爪哇) - how do I find the smallest number in a text file? (Java) 如何在不使用任何数组的情况下找到浮点数列表中的最小数字? - How do I find the smallest number in a list of floats without using any arrays? 我如何在 Java 中创建对象列表 - How do i create a list of Objects in Java 如何从 Java 中的列表中找到过滤元素的平均值 - How do I find the average of filtered elements from list in Java 如何在运行时过滤 Java 对象列表,其中我正在过滤的变量事先未知? - How do I filter a list of java objects at runtime where the variable I am filtering on is not known in advance? 你如何在java文本文件中找到最大和最小的数字? - How do you find the largest and smallest numbers in a java text file? Java,我如何搜索保存在数组列表中的对象的特定变量 - Java, how do I search a specific variable of Objects saved in a array list 如何从用户输入中找到最大的数字和最小的数字? (同时循环) - How do I find the largest number and the smallest number from the user input? (while loop)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM