我对Java很陌生,并且承担了一些我坚持的工作。

任务是编写一个Java程序来查找组中最短和最高的人员,其中该组的人数未知。该程序将询问每个人的名字和身高(以米为单位)。高度应在0.8m至2.5m的范围内进行验证。然后,程序必须显示组中最矮和最高的人的姓名和身高。另外,不允许使用数组和ArrayLists。

如果有人能指出我正确的方向,我将不胜感激,因为目前我似乎无法解决这个问题。

编辑:我在下面一些有用的注释后为程序编写了代码,除非我在打印空值的程序时遇到问题。下面是代码。

package pracset1;

import java.util.Scanner;

public class shortTall {

public static void main(String[] args) {
    // TODO Auto-generated method stub

    Scanner keyboard = new Scanner(System.in);

    String name;
    String maxname = null;
    String minname = null;
    double height;
    double maxheight = -1.0d;
    double minheight = 1000.0d;
    char addperson;
    String choice;

    do
    {
        System.out.println("Enter first name of person");
        name = keyboard.next();
        System.out.println("Enter height of person");
        height = keyboard.nextDouble();

        if (height < 0.8 || height > 2.5)
        {
            System.out.println("Entered height must be above 0.8m and below 2.5m");
            height = keyboard.nextDouble();
        }

        if (height > maxheight)
        {
            maxheight = height;
            maxname = name;
        }
            else if (height < minheight)
            {
                minheight = height;
                minname = name;
            }

        System.out.println("Do you want to add another person? (Y/N)");
        choice = keyboard.next();
        addperson = choice.charAt(0);
    }
        while (addperson == 'Y' || addperson == 'y');   //

    System.out.println("Tallest person is " + maxname + " with a height of " + maxheight);
    System.out.println("Shortest person is " + minname + " with a height of " + minheight);

}
}


下面是问题的屏幕截图

java - 确定组中最高/最矮的人-LMLPHP

最佳答案

像这样:

double maxheight = -1.0d;
double minheight = 1000.0d;
String maxname = "";
String minname = "";

while (true)
{
    String name = System.console().readLine();
    double height = Double.parseDouble(System.console().readLine());

    if(height < 0.8 || height > 2.5)
    {
        System.out.println("%d is to large or to small", height);
        continue;
    }

    if (height > maxheight)
    {
        maxname = name;
        maxheight = height;
    }

    if (height < minheight)
    {
        minname = name;
        minheight = height;
    }

    System.out.println("%s is heighest person with %d meters", maxname, maxheight);
    System.out.println("%s is lowest person with %d meters", minname, minheight);
}


您需要做一些其他的事情,例如让用户退出程序。但是你知道吗?

编辑:

该代码应如下所示:

package pracset1;

import java.util.Scanner;

public class shortTall {

    public static void main(String[] args) {
        // TODO Auto-generated method stub

        Scanner keyboard = new Scanner(System.in);

        String name;
        String maxname = null;
        String minname = null;
        double height;
        double maxheight = -1.0d;
        double minheight = 1000.0d;
        char addperson;
        String choice;

        do
        {
            System.out.println("Enter first name of person");
            name = keyboard.next();
            System.out.println("Enter height of person");
            height = keyboard.nextDouble();

            while (height < 0.8 || height > 2.5)
            {
                System.out.println("Entered height must be above 0.8m and below 2.5m");
                height = keyboard.nextDouble();
            }

            if (height > maxheight)
            {
                maxheight = height;
                maxname = name;
            }

            if (height < minheight)
            {
                minheight = height;
                minname = name;
            }

            System.out.println("Do you want to add another person? (Y/N)");
            choice = keyboard.next();
            addperson = choice.charAt(0);
        }
        while (addperson == 'Y' || addperson == 'y');   //

        System.out.println("Tallest person is " + maxname + " with a height of " + maxheight);
        System.out.println("Shortest person is " + minname + " with a height of " + minheight);

    }
}


通过将第一个if语句更改为while循环。输入有效高度后,程序才能继续执行。

通过将最后一个else if语句更改为if语句。输入的第一个有效人员将同时成为最高和最小的人员。

关于java - 确定组中最高/最矮的人,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/25401006/

10-11 08:17