用户输入一个接一个地循环运行两个循环

时间:2016-01-27 07:30:58

标签: java while-loop do-while

在我的方法中,我想要用户输入,如果它首先传递,而它需要转到第二个while循环,在第二个,如果条件失败,它需要重新运行循环,而不是重新运行自己,第一个同时正在执行。

//Method to read, validate and store postcode and purchase amount for N customers
public  void addRecord(int customerCounter) throws IOException
{
    int postcode;
    double purchaseAmount;

    int conform =0;

    do{
        BufferedReader breader= new BufferedReader(new InputStreamReader(System.in));
        System.out.print("Postcode: ");
        postcode= Integer.parseInt(breader.readLine());

        if(4121 >postcode)
        {
            System.out.print("Postcode must be greater than 4121");
            System.out.print("Try again: ");                            
        }
        else if(postcode > 4123)
        {
            System.out.print("Postcode must be less than 4123");
            System.out.print("Try again: ");                            
        }
        else
        {
            conform = 1;
            //System.out.println(conform);
        }
    }
    while(4121 >postcode && postcode > 4123);



    if(conform == 1)
    {
        System.out.println(conform);
        do
        {
            BufferedReader breader2= new BufferedReader(new InputStreamReader(System.in));
            breader2= new BufferedReader(new InputStreamReader(System.in));
            System.out.println("Purchase Amount: ");
            purchaseAmount= Double.parseDouble(breader2.readLine());

            if(purchaseAmount < 60)
            {
                System.out.print("Purchase Amount must be greater than $60");
                System.out.print("Try again: ");
                continue;
            }
            else if(purchaseAmount > 500)
            {
                System.out.print("Purchase Amount must be greater than $500");
                System.out.print("Try again: ");                                                
            }           
        }
        while(purchaseAmount < 60 && purchaseAmount > 500);         

    }   

}

2 个答案:

答案 0 :(得分:1)

while中的条件是错误的,它们总是错误的

postcode = 5000;
while(postcode < 4121 && postcode > 4123); -> while(false && true); -> false

postcode = 3000;
while(postcode < 4121 && postcode > 4123); -> while(true && false); -> false

使用OR而不是AND

while(postcode < 4121 || postcode > 4123);

第二次同样如此。

第一次执行第二次可能是因为您再次致电addRecord

答案 1 :(得分:0)

最好的解决方案是使用2个嵌套的无限循环。

通过这种方式,程序只检查邮政编码的2倍和为每个循环插入的价格,以确定是否可以继续,而不是解决方案中的4次。

//Method to read, validate and store postcode and purchase amount for N customers
public  void addRecord(int customerCounter) throws IOException
{
    int postcode;
    double purchaseAmount;

    int conform =0;

    do{
        BufferedReader breader= new BufferedReader(new InputStreamReader(System.in));
        System.out.print("Postcode: ");
        postcode= Integer.parseInt(breader.readLine());

        if(4121 >postcode)
        {
            System.out.print("Postcode must be greater than 4121");
            System.out.print("Try again: ");                            
        }
        else if(postcode > 4123)
        {
            System.out.print("Postcode must be less than 4123");
            System.out.print("Try again: ");                            
        }
        else
        {
            System.out.println(conform);
            do{

                BufferedReader breader2= new BufferedReader(new InputStreamReader(System.in));
                breader2= new BufferedReader(new InputStreamReader(System.in));
                System.out.println("Purchase Amount: ");
                purchaseAmount= Double.parseDouble(breader2.readLine());

                if(purchaseAmount < 60)
                {
                    System.out.print("Purchase Amount must be greater than $60");
                    System.out.print("Try again");
                    continue;
                 }
                 else if(purchaseAmount > 500)
                 {
                     System.out.print("Purchase Amount must be greater than $500");
                     System.out.print("Try again");                                                
                 }else{
                     break;
                 }

             }while(true);
             break;
        }
    }
    while(true);

}