如何在循环中显示最老用户的信息?

时间:2015-12-10 09:43:37

标签: java

我编写了一个程序,允许用户输入手头的病人数。然后程序启动一个循环,询问每个患者的姓名和年龄,并显示患者'平均年龄。唯一的问题是我不知道如何在计划结束时显示最年轻和最老的患者的信息。这是代码,有人可以帮我这个吗?

public static void main(String[] args) {
    int i, i4, a;
    String s1, s2, s3, s4;

    s1 = JOptionPane.showInputDialog("Enter The Number Of Patients");
    i = Integer.parseInt(s1);
    a = 0;
    long totalAges = 0;
    while (a < i) {
        s2 = JOptionPane.showInputDialog("Enter patient's ID");

        s3 = JOptionPane.showInputDialog("Enter patient's Name");

        s4 = JOptionPane.showInputDialog("Enter patient's Age");
        i4 = Integer.parseInt(s4);

        JOptionPane.showMessageDialog(null, " ID : " + s2 + "\n Name : " + s3 + "\n Age : " + i4);

        totalAges += i4;
        a++;
    }

    double avgAge = ((double) totalAges) / i;
    JOptionPane.showMessageDialog(null, " Average Age: " + avgAge);
}

4 个答案:

答案 0 :(得分:0)

您可以在循环外部创建变量来处理这个变量,这些变量也处理最早和最年轻的人名和年龄,并将其与当前新输入的内容进行比较:

public static void main(String[] args) {
int i, i2, i4, a;
String s1, s2, s3, s4;
int youngestAge, oldestAge;
String youngestName = ""; 
String oldestName = "";



s1 = JOptionPane.showInputDialog("Enter The Number Of Patients");
i = Integer.parseInt(s1);
a = 0;
long totalAges = 0;
while (a < i) {
    s2 = JOptionPane.showInputDialog("Enter patient's ID");
    i2 = Integer.parseInt(s2);
    s3 = JOptionPane.showInputDialog("Enter patient's Name");

    s4 = JOptionPane.showInputDialog("Enter patient's Age");
    i4 = Integer.parseInt(s4);

    if(i4 < youngestAge){
      youngestAge = i4;
      youngestName = s3;
    } else if(i4 > oldestAge){
      oldestAge = i4;
      oldestName = s3;
    }

    JOptionPane.showMessageDialog(null, " ID : " + i2 + "\n Name : " + s3 + "\n Age : " + i4);

    totalAges += i4;
    a++;
}

double avgAge = ((double) totalAges) / i;
JOptionPane.showMessageDialog(null, " Average Age: " + avgAge);

答案 1 :(得分:0)

跟踪您最年轻和最老的患者:

int youngest = 200;
int oldest = 0;
while (a < i) {
    s2 = JOptionPane.showInputDialog("Enter patient's ID");
    i2 = Integer.parseInt(s2);
    s3 = JOptionPane.showInputDialog("Enter patient's Name");

    s4 = JOptionPane.showInputDialog("Enter patient's Age");
    i4 = Integer.parseInt(s4);

    if (i4 < youngest) {
        youngest = i4;
    }

    if (i4 > oldest) {
        oldest = i4;
    }

    JOptionPane.showMessageDialog(null, " ID : " + i2 + "\n Name : " + s3 + "\n Age : " + i4);

    totalAges += i4;
    a++;
}

如果要检索整个患者数据,您必须创建一个具有属性名称,ID和年龄的类患者,并在循环中跟踪他们的ID(假设ID是唯一的)

答案 2 :(得分:0)

如果您需要跟踪有关最年轻和最年长患者的所有信息,请考虑创建一个Patient类(在此示例中,患者是主要方法中的本地类,但您可以将其删除如果你需要)

    public static void main(String[] args) {

        class Patient{

            private int id;
            private int age;
            private String name;

        }

        int i, i2, i4, a;
        String s1, s2, s3, s4;

        s1 = JOptionPane.showInputDialog("Enter The Number Of Patients");
        i = Integer.parseInt(s1);
        a = 0;
        long totalAges = 0;

        Patient youngerPatient=null;
        Patient olderPatient=null;

        while (a < i) {

            Patient patient = new Patient();

            s2 = JOptionPane.showInputDialog("Enter patient's ID");
            i2 = Integer.parseInt(s2);
            s3 = JOptionPane.showInputDialog("Enter patient's Name");

            s4 = JOptionPane.showInputDialog("Enter patient's Age");
            i4 = Integer.parseInt(s4);

            patient.id=i2;
            patient.name=s3;
            patient.age=i4;

            if(youngerPatient==null || youngerPatient.age > patient.age)
                youngerPatient = patient;

            if(olderPatient==null || olderPatient.age < patient.age)
                olderPatient = patient;

            JOptionPane.showMessageDialog(null, " ID : " + i2 + "\n Name : " + s3 + "\n Age : " + i4);

            totalAges += i4;
            a++;
        }

        double avgAge = ((double) totalAges) / i;
        JOptionPane.showMessageDialog(null, " Average Age: " + avgAge+ " Younger patient:" +youngerPatient.name+ " Older patient:" +olderPatient.name);

}

此外,如果您想跟踪所有患者,可以改用阵列。

答案 3 :(得分:0)

您需要添加两个额外的整数来跟踪最年龄和最大年龄。将Integer.MAX_VALUE分配给最年轻的年龄,将Integer.MIN_VALUE分配给最早的年龄。 (这些是常量,并且被许多人认为是比编写其他任意数字更好的编程风格。)然后检查每个输入的年龄是否是新的最老年龄或新的最年龄,并且如果是,则替换适当的变量。

这是一个完整的工作计划:

import javax.swing.JOptionPane;

public class Patients
{
  public static void main(String[] args)
  {
    int i, i4, a;
    String s1, s2, s3, s4;

    s1 = JOptionPane.showInputDialog("Enter The Number Of Patients");
    i = Integer.parseInt(s1);
    a = 0;
    long totalAges = 0;
    int youngestAge = Integer.MAX_VALUE;
    int oldestAge = Integer.MIN_VALUE;
    while (a < i)
    {
      s2 = JOptionPane.showInputDialog("Enter patient's ID");

      s3 = JOptionPane.showInputDialog("Enter patient's Name");

      s4 = JOptionPane.showInputDialog("Enter patient's Age");
      i4 = Integer.parseInt(s4);

      JOptionPane.showMessageDialog(null, " ID : " + s2 + "\n Name : " + s3 + "\n Age : " + i4);

      if (i4 > oldestAge)
        oldestAge = i4;
      if (i4 < youngestAge)
        youngestAge = i4;

      totalAges += i4;
      a++;
    }
    double avgAge = ((double) totalAges) / i;
    JOptionPane.showMessageDialog(null, " Average Age: " + avgAge +
        "\nYoungest age: " + youngestAge + "\nOldest age: " + oldestAge);
  }
}