Я должен запрограммировать программу, которая собирает высоты и серийный номер и выводит maxq мин. - PullRequest
0 голосов
/ 26 декабря 2018

Мне нужно запрограммировать программу, которая подбирает количество актрис (число 1, 2 и т. Д.) И их рост, чтобы я наконец получил серийный номер самой высокой и самой короткой актрисы и их рост.Я не совсем уверен, как это сделать, вот что я написал, но это не работает:

import java.util.* ;
public class While
{
    public static void main ( String[ ]  args )
    {
        Scanner io = new Scanner (System.in) ;
        float min= Float.MAX_VALUE;
        float max= Float.MIN_VALUE;
        float max2= Float.MIN_VALUE;
        float min2= Float.MAX_VALUE;
        System.out.print ( "Enter the actress serial number:" ) ;
        int num= io.nextInt ( );
        System.out.print ( "Enter the actress height:" ) ;
        float height = io.nextFloat ( ) ;
        while  ( height != -1 )
        {           
            System.out.println ( "Enter the next actress serial number:");
            System.out.println ( "Enter the next actress height, When you're finished, press -1:");
            if (height>max)
                max= height;
            if (height<min)
                min= height;
            if (num >max2)
                max2= num;
            if (num< min2)
                min= num;
            num= io.nextInt ( );
            height = io.nextFloat ( ) ;
        }
        System.out.println ( ) ;
        System.out.println ("The selected actress are:" +max2 +" ," +min2); 
        System.out.println ("Their height respectively:" +min +", " +max ) ;
    }
}

1 Ответ

0 голосов
/ 26 декабря 2018

Создайте класс Actor следующим образом:

public class Actor {
    int id;
    long serialNumber;
    String name;
    double height;

    //Constructor with parameters
    public Actor(int id, long serialNumber, String name, double height) {
        id=Id;
        serialNumber=SerialNumber;
        name =Name;
        height= Height;
    }

    public int getId() {
        return id;
    }
    public void setId(int id) {
        this.id = id;
    }
    public long getSerialNumber() {
        return serialNumber;
    }
    public void setSerialNumber(long serialNumber) {
        this.serialNumber = serialNumber;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public double getHeight() {
        return height;
    }

    public void setHeight(double height) {
        this.height = height;
    }

}

Это пример использования класса Actor.Обратите внимание, что этот код может быть улучшен.

public class Main {

    public static void main(String[] args) {
    // Create some actresses
        Actor actress1= new Actor(1,221114445, "Tracy",5.1);
        Actor actress2= new Actor(2,332555412, "Ann", 6.0);
        Actor actress3= new Actor(3,200011458, "Rebecca", 5.8);
        Actor actress4= new Actor(4,701221496, "Marcy", 5.3);

        //Calculate maximum and minimum heights
        double maxHeight = Math.max(Math.max(actress1.height, actress2.height),
                Math.max(actress3.height, actress4.height));
        double minHeight = Math.min(Math.min(actress1.height, actress2.height),
                Math.min(actress3.height, actress4.height));

        //Determine the tallest
        if (maxHeight==actress1.height) {
            System.out.println("Actress "+actress1.name +" "
                    + "is the tallest with a height of : "+actress1.height);
        }
        if (maxHeight==actress2.height) {
            System.out.println("Actress "+actress2.name +" "
                    + "is the tallest with a height of : "+actress2.height);
        }

        if (maxHeight==actress3.height) {
            System.out.println("Actress "+actress3.name +" "
                    + "is the tallest with a height of : "+actress3.height);
        }

        if (maxHeight==actress4.height) {
            System.out.println("Actress "+actress4.name +" "
                    + "is the tallest with a height of : "+actress4.height);
        }

        //Determine the shortest
                if (minHeight==actress1.height) {
                    System.out.println("Actress "+actress1.name +" "
                            + "is the shortest with a height of : "+actress1.height);
                }
                if (minHeight==actress2.height) {
                    System.out.println("Actress "+actress2.name +" "
                            + "is the shortest with a height of : "+actress2.height);
                }

                if (minHeight==actress3.height) {
                    System.out.println("Actress "+actress3.name +" "
                            + "is the shortest with a height of : "+actress3.height);
                }

                if (minHeight==actress4.height) {
                    System.out.println("Actress "+actress4.name +" "
                            + "is the shortest with a height of : "+actress4.height);
                }

    }
}
...