Java:如何重复输入直到收到所需的输入?

时间:2016-09-09 03:48:38

标签: java loops

我已经阅读了很多关于while循环等的帖子和页面,我只是无法理解在哪里放置一个循环来重复这个,直到满足三个条件中的一个。如果有人可以指向我需要添加一些时间的地方,或者我会非常感激的任何地方,除非您愿意,否则无需打印任何代码。

    System.out.print("Customer name: ");
    name=scan.next();
    System.out.print("Customer member level: ");
    level=scan.next();
    if (level.equalsIgnoreCase("Jedi"))
    {
        System.out.print("Current balance: ");
        balance=scan.nextDouble();
        System.out.print("Was the payment made late?: ");
        late=scan.next();
        if (late.equalsIgnoreCase("Yes"))
        {
            double interestLatePayment=(balance*0.02);
            paymentToPrinciple=(balance*0.04);
            totalPayment=(interestLatePayment+paymentToPrinciple);
            percentToPrinciple=100*(paymentToPrinciple/totalPayment);
            percentToInterest=(100-percentToPrinciple);

            System.out.println("\nBill for "+level+" customer "+name);
            System.out.printf("Card Balance: $%1.2f%n", balance);
            System.out.printf("Interest for late payment (2.0%% of principle): $%1.2f%n", interestLatePayment);
            System.out.printf("Minimum payment to principle (4%% of principle): $%1.2f%n", paymentToPrinciple);
            System.out.printf("Minimum total payment (payment and fees): $%1.2f%n", totalPayment);
            System.out.printf("Percent to principle: %1.1f%%n", percentToPrinciple);
            System.out.printf("Percent to interest and fees: %1.1f%%n", percentToInterest);
        }
        else if(late.equalsIgnoreCase("No"))
        {
            double interestLatePayment=(balance*0.01);
            paymentToPrinciple=(balance*0.04);
            totalPayment=(interestLatePayment+paymentToPrinciple);
            percentToPrinciple=100*(paymentToPrinciple/totalPayment);
            percentToInterest=(100-percentToPrinciple);

            System.out.println("\nBill for "+level+" customer "+name);
            System.out.printf("Card Balance: $%1.2f%n", balance);
            System.out.printf("Interest for on-time payment (1.0%% of principle): $%1.2f%n", interestLatePayment);
            System.out.printf("Minimum payment to principle (4%% of principle): $%1.2f%n", paymentToPrinciple);
            System.out.printf("Minimum total payment (payment and fees): $%1.2f%n", totalPayment);
            System.out.printf("Percent to principle: %1.1f%%n", percentToPrinciple);
            System.out.printf("Percent to interest and fees: %1.1f%%n", percentToInterest);
        }
    }
    if (level.equalsIgnoreCase("Padowan"))
    {
        System.out.print("Current balance: ");
        balance=scan.nextDouble();
        System.out.print("Was the payment made late?: ");
        late=scan.next();
        if (late.equalsIgnoreCase("Yes"))
        {
            double interestLatePayment=(balance*0.028);
            paymentToPrinciple=(balance*0.04);
            totalPayment=(interestLatePayment+paymentToPrinciple);
            percentToPrinciple=100*(paymentToPrinciple/totalPayment);
            percentToInterest=(100-percentToPrinciple);
            lateFee=10;

            System.out.println("\nBill for "+level+" customer "+name);
            System.out.printf("Card Balance: $%1.2f%n", balance);
            System.out.printf("Interest for late payment (2.8%% of principle): $%1.2f%n", interestLatePayment);
            System.out.printf("Late fee: $%1.2f%n", lateFee);
            System.out.printf("Minimum payment to principle (4%% of principle): $%1.2f%n", paymentToPrinciple);
            System.out.printf("Minimum total payment (payment and fees): $%1.2f%n", totalPayment);
            System.out.printf("Percent to principle: %1.1f%%n", percentToPrinciple);
            System.out.printf("Percent to interest and fees: %1.1f%%n", percentToInterest);
        }
        else if(late.equalsIgnoreCase("No"))
        {
            double interestLatePayment=(balance*0.018);
            paymentToPrinciple=(balance*0.04);
            totalPayment=(interestLatePayment+paymentToPrinciple);
            percentToPrinciple=100*(paymentToPrinciple/totalPayment);
            percentToInterest=(100-percentToPrinciple);

            System.out.println("\nBill for "+level+" customer "+name);
            System.out.printf("Card Balance: $%1.2f%n", balance);
            System.out.printf("Interest for late payment (1.8%% of principle): $%1.2f%n", interestLatePayment);
            System.out.printf("Minimum payment to principle (4%% of principle): $%1.2f%n", paymentToPrinciple);
            System.out.printf("Minimum total payment (payment and fees): $%1.2f%n", totalPayment);
            System.out.printf("Percent to principle: %1.1f%%n", percentToPrinciple);
            System.out.printf("Percent to interest and fees: %1.1f%%n", percentToInterest);
        }
    }
    if (level.equals("Youngling"))
    {
        System.out.print("Current balance: ");
        balance=scan.nextDouble();
        System.out.print("Was the payment made late?: ");
        late=scan.next();
        if (late.equalsIgnoreCase("Yes"))
        {
            double interestLatePayment=(balance*0.035);
            paymentToPrinciple=(balance*0.04);
            totalPayment=(interestLatePayment+paymentToPrinciple);
            percentToPrinciple=100*(paymentToPrinciple/totalPayment);
            percentToInterest=(100-percentToPrinciple);
            lateFee=20;

            System.out.println("\nBill for "+level+" customer "+name);
            System.out.printf("Card Balance: $%1.2f%n", balance);
            System.out.printf("Interest for late payment (3.5%% of principle): $%1.2f%n", interestLatePayment);
            System.out.printf("Late fee: $%1.2f%n", lateFee);
            System.out.printf("Minimum payment to principle (4%% of principle): $%1.2f%n", paymentToPrinciple);
            System.out.printf("Minimum total payment (payment and fees): $%1.2f%n", totalPayment);
            System.out.printf("Percent to principle: %1.1f%%n", percentToPrinciple);
            System.out.printf("Percent to interest and fees: %1.1f%%n", percentToInterest);
        }
        else if(late.equalsIgnoreCase("No"))
        {
            double interestLatePayment=(balance*0.025);
            paymentToPrinciple=(balance*0.04);
            totalPayment=(interestLatePayment+paymentToPrinciple);
            percentToPrinciple=100*(paymentToPrinciple/totalPayment);
            percentToInterest=(100-percentToPrinciple);

            System.out.println("\nBill for "+level+" customer "+name);
            System.out.printf("Card Balance: $%1.2f%n", balance);
            System.out.printf("Interest for late payment (2.5%% of principle): $%1.2f%n", interestLatePayment);
            System.out.printf("Minimum payment to principle (4%% of principle): $%1.2f%n", paymentToPrinciple);
            System.out.printf("Minimum total payment (payment and fees): $%1.2f%n", totalPayment);
            System.out.printf("Percent to principle: %1.1f%%n", percentToPrinciple);
            System.out.printf("Percent to interest and fees: %1.1f%%n", percentToInterest);
        }
    }
    else 
    {
        System.out.print("Invalid member level. Enter valid member level: ");
    }
}

}

2 个答案:

答案 0 :(得分:1)

这样的事情:

String input;
do {
  input = getNewInput(); //replace this with however you get input, probably a scanner
}while (!input.equals("firstcondition") && !input.equals("secondcondition"))

//then after that do whatever you want to with the input now that you know it matches

答案 1 :(得分:0)

像这样的东西。你必须弄明白自己,if-results是你阻止它的触发器。

 while(true){
     System.out.print("Customer name: ");
     name=scan.next();
     System.out.print("Customer member level: ");
     level=scan.next();
     if (level.equalsIgnoreCase("Jedi"))
     {

         if (late.equalsIgnoreCase("Yes"))
         {
             // your code
             break;
         }
         else if(late.equalsIgnoreCase("No"))
         {
             // your code
         }
     }
     if (level.equalsIgnoreCase("Padowan"))
     {
         if (late.equalsIgnoreCase("Yes"))
         {
             // your code
             break;
         }
         else if(late.equalsIgnoreCase("No"))
         {
            // your code
         }
     }
     if (level.equals("Youngling"))
     {

         if (late.equalsIgnoreCase("Yes"))
         {
             // your code
             break;
         }
         else if(late.equalsIgnoreCase("No"))
         {
             // your code
         }
     }
     else 
     {
         System.out.print("Invalid member level. Enter valid member level: ");
     }
 }