检查谁的人数最多并送礼物给他们

时间:2019-06-05 14:43:59

标签: java

我正在尝试找出人数最多的人,并将礼物送给人数最多的人。考虑是否有4个人,输入no为n1,n2,n3,n4。然后检查哪个人的NO最大,然后给他们礼物。我只是试图在4个数字中找到最大的。

int great=0;
if (n1 >= n2 && n1 >= n3 && n1>=n4)
    great=n1;
else if (n2 >= n1 && n2 >= n3 && n2>=n4)
    great=n2;
else if(n3>=n1 && n3>=n4)
    great=n3;
else
    great=n4;

如果假设3个人的最高分相同,则不给3个人的礼物。将输出打印为“好,你得到一份礼物”,以及与他们相处得最大的人的名字。

4 个答案:

答案 0 :(得分:0)

首先找到最大值:

int[] all = new int[] { n1, n2, n3, n4 };
int max = -1;
for (int i=0; i<all.length; i++) {
    if (max < all[i) {
        max = all[i];
    }
}

然后查看谁拥有最大值,并打印索引,名称或其他内容

for (int i=0; i<all.length; i++) {
    if (max == all[i) {
        System.out.println ("Good you got a gift " + i);
    }
}

答案 1 :(得分:0)

Integer[] a = {105, 30, 40, 110};
Arrays.sort(a, Collections.reverseOrder());
int great = a[0];
System.out.println("Good you got a gift: "+ great);

尝试一下

答案 2 :(得分:0)

也许这可以为您工作。我认为您应该去参加一个有名字和数字的人类,因为最后您要显示数字最大的人的名字。

/**
 * Class representing each person
 */
public class Person{

    private final String name;
    private final Integer number;

    Person(String name, Integer number)
    {
        this.name = name;
        this.number = number;
    }

    public String getName() {
        return name;
    }

    public Integer getNumber() {
        return number;
    }

    @Override
    public String toString() {
        return name;
    }
}

这是另一堂课。请为此创建另一个文件。

public class Main{
/**
 * This is the main method
 */
public static void main(String[] args) {

    List<Person>all = new ArrayList<>();
    all.add(new Person("P1", 5));
    all.add(new Person("P2", 2));
    all.add(new Person("P3", 3));
    all.add(new Person("P4", 5));

    Integer max = findMaxNumber(all);

    List<Person> winners = findWinners(all, max);

    System.out.print("Good you got a gift: " + winners);
}

private static Integer findMaxNumber(List<Person> all) {
    Integer max = 0;

    for(Person onePerson : all)
    {
        Integer onePersonNumber = onePerson.getNumber();
        if(onePersonNumber > max)
        {
            max = onePersonNumber;
        }
    }
    return max;
}

private static List<Person> findWinners(List<Person> all, Integer max) {
    List<Person>winners = new ArrayList<>();
    for(Person onePerson : all)
    {
        if(onePerson.getNumber().equals(max))
        {
            winners.add(onePerson);
        }
    }
    return winners;
}

}//end of Main Class

--------------------------------------------------- --------------------

这是第二种解决方案。就像第一个只使用lambda表达式的人一样。

/**
 * Class representing each person
 */
public class Person{

    private final String name;
    private final Integer number;

    Person(String name, Integer number)
    {
        this.name = name;
        this.number = number;
    }

    public String getName() {
        return name;
    }

    public Integer getNumber() {
        return number;
    }

    @Override
    public String toString() {
        return name;
    }
}

这是另一堂课。请为此创建另一个文件。

public class Main{
/**
 * This is the main method
 */
public static void main(String[] args) {

    List<Person>all = new ArrayList<>();
    all.add(new Person("P1", 5));
    all.add(new Person("P2", 2));
    all.add(new Person("P3", 5));
    all.add(new Person("P4", 5));

    Integer max = findMaxNumber(all);

    List<Person> winners = findWinners(all, max);

    System.out.print("Good you got a gift: " + winners);
}

private static Integer findMaxNumber(List<Person> all)
{
    /**
     * Map from Person to Integer (Person's number) and find the max number. 
     */
    OptionalInt maxNum = all.stream()
                            .mapToInt(p -> p.getNumber())
                            .max();
    return maxNum.getAsInt();
}

private static List<Person> findWinners(List<Person> all, Integer max) {
    /**
     * Filter the people that have the max number and collect them in the list
     */
    List<Person> winners = all.stream()
                              .filter(p -> p.getNumber().equals(max))
                              .collect(Collectors.toList());
    return winners;
}


}//end of Main Class

答案 3 :(得分:0)

您可以简单地使用filter { json_encode { source => "detail" target => "detail" } json { source => "detail" remove_field => [ "detail" ] } } IntStream方法:

max