从键盘的用户输入填充ArrayList

时间:2013-11-30 04:47:10

标签: java

我编写了这段代码,试图填充一个ArrayList来测量收缩压,舒张压和键盘上用户输入的日期。我不知道如何在这个arraylist中填充多个元素。请有人帮忙。

读数: 65 98 17/12/2013

import java.util.ArrayList;
import java.util.InputMismatchException;
import java.util.Scanner;



public class BloodTest {



            private static int input1;
            private static int input2;
            private static int num1;
            private static int num2;
            private static int num3;

public static void main(String[] args) {
    ArrayList<Blood>mary = new ArrayList<Blood>();
    Scanner sc = new Scanner(System.in);
    boolean repeat = true;

    //Getting input from user
    do
    {
        try
        {
            System.out.printf("%s","Please enter your systolic blood pressure: ");
            int input1 = sc.nextInt();
            System.out.printf("%s","Please enter your diastolic blood pressure: ");
            int input2 = sc.nextInt();

            //Requires user input for day (dd) to be between 1-31
                boolean try1 = true;
                do{
                    System.out.printf("%s","Please enter the day (dd): ");
                    int input = sc.nextInt();
                    if(0<input && input<= 31)
                      {
                       num1 = input;
                       try1 = false;
                      }
                    else
                    System.out.printf("\n%s\n", "Please enter a value for day (dd) between 1-31:");
                   }
                while (try1);

            //Requires user input for month (mm) to be between 1-12
                boolean try2 = true;
                do{
                    System.out.printf("%s","Please enter the month (mm): ");
                    int input = sc.nextInt();
                    if(0<input && input<= 12)
                      {
                       num2 = input;
                       try2 = false;
                      }
                    else
                    System.out.printf("\n%s\n", "Please enter a value for month (mm) between 1-12:");
                   }
                while (try2);

              //Requires user input for year (yyy) to be between 1-9999

                boolean try3 = true;
                do{
                    System.out.printf("%s","Please enter the year (yyyy): ");
                    int input = sc.nextInt();
                    if(0<input && input<= 9999)
                      {
                       num3 = input;
                       try3 = false;
                      }
                    else
                    System.out.printf("\n%s\n", "Please enter a value for year (yyyy) between 1-9999:");
                   }
                while (try3);


            mary.add(new Blood(input1, input2, new Day(num1, num2, num3)));

            repeat = false;
        }
        catch (InputMismatchException inputMismatchException)
        {
            System.err.printf("\n%s\n\n",inputMismatchException);
            sc.nextLine();
            System.out.println("Please enter a integer value:");
        }

    }
    while (repeat);


    //Displaying arraylist data so far
    for (int i = 0; i<mary.size(); i++)
    System.out.println(mary.get(i));

}

}

4 个答案:

答案 0 :(得分:0)

您只需向ArrayList添加一个元素。它不在循环中,只在尝试中。 .add()可以添加,并且您也可以很好地浏览列表。

答案 1 :(得分:0)

你可能不应该只停留repeat = false; ...也许检查一下你的Scanner.hasNext()

答案 2 :(得分:0)

仅当用户输入一些值(表示输入结束)时才执行'repeat = false',例如: 问用户,你想继续吗?如果'重复=假',则答案为'N'。

答案 3 :(得分:0)

ArrayList Population示例。

import java.util.ArrayList;
import java.util.Scanner;
class Blood{
    private double systolic;
    public  double getSystolic() {
        return systolic;
    }

    public  void setSystolic(double systolic) {
        this.systolic = systolic;
    }
}
public class PoupulateArray {
    public static void main(String[] args) {
        ArrayList<Blood> bloodList=new ArrayList<Blood>();
        Scanner s=new Scanner(System.in);
        Blood blood=null;
        boolean proceed=true;
        do {
            blood=new Blood();
            System.out.println("Enter Systolic:");
            double systolic=s.nextDouble();
            blood.setSystolic(systolic);
            bloodList.add(blood);
            System.out.println("Continue?y/n");
            String option=s.next();
            if(option.equals("n")){
                proceed=false;
            }
        } while (proceed==true);

        for (Blood blood2 : bloodList) {
            System.out.println(blood2.getSystolic());
        }

    }
}