Java使用数组Double重复比较值,并在输入错误后重复

时间:2019-05-16 10:37:52

标签: java

当用户输入价值支付率时,它将用支付率数组检查价值支付率。如果正确,则它将提示用户是否需要执行其他过程。如果没有,它将进行计算。

如果工资率错误,则会显示错误并要求用户再次输入值。

我遇到的问题是,在用户按下“ Y”后,它将显示“错误的工资率,请重试”。

from django import template

register = template.Library()


@register.filter
def multiply(value, arg):
    return (value * arg)

结果:

enter image description here

2 个答案:

答案 0 :(得分:2)

您应该执行System.out.println("You have entered the wrong pay rate. Please try again");行,仅在 isPayRatefalse的情况下。

boolean finished = false;
do {
    System.out.print("Enter pay rate: ");
    payRateEntered = read.nextDouble();

    for (int i = 0; i < payRateDefault.length; i++) {

        if (payRateDefault[i] == payRateEntered) {

            //If the payRate is true with array payRateDefault, proceed to ask if you have another employee
            System.out.println("Do you have any employee to process (Y/N)");
            anotherProcess = read.next().charAt(0);
            isPayRate = true;

            //Check if Y or N
            switch (anotherProcess) {
                case 'Y':
                    //Proceed back to prompt user to enter pay rate

                    break;
                case 'N':
                    //Proceed to calculation
                    finished = true;
                    break;
                default:
                    //If wrong input
                    System.out.println("Please use Y or N only");
                    break;
            }
            break;
        } else {
            isPayRate = false;
        }
    }
    if (!isPayRate) {
        System.out.println("You have entered the wrong pay rate. Please try again");
    }
} while (!finished);

答案 1 :(得分:1)

以下代码对我来说很好:

package mm.com.java.so.test;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class JavaRepeat {

    public static void main(String[] args) throws IOException {

        BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));

        double[] defaultPayRates = { 3.50, 4.00, 4.50, 4.75, 5.00, 5.25, 5.50, 5.75, 6.00 };

        double payRate;
        boolean isValid;
        boolean isContinue;
        String next;
        int workingHour;

        do {

            isValid = false;
            isContinue = false;

            System.out.print("\nEnter hours work : ");

            workingHour = Integer.parseInt(reader.readLine());

            do {

                System.out.print("Enter pay rate: ");

                payRate = Double.parseDouble(reader.readLine());

                for (int i = 0; i < defaultPayRates.length; i++) {

                    if (defaultPayRates[i] == payRate) {
                        isValid = true;
                        // TODO : make calculation here.
                        break;
                    }
                }

                if (!isValid) {
                    System.out.println("You have entered the wrong pay rate. Please try again !!!");
                }

            } while (!isValid);

            do {

                isValid = true;

                System.out.println("\nDo you have any employee to process (Y/N)");

                next = reader.readLine();

                switch (next.toLowerCase()) {
                    case "y":
                        isContinue = true;
                        break;

                    case "n":
                        isContinue = false;
                        break;

                    default:
                        isValid = false;
                        System.out.println("Please use Y or N only.");
                        break;
                }
            } while (!isValid);

        } while (isContinue);

        // TODO : print out calculation here.

        System.out.println("\nCalculation is doing. Please wait...");
    }
}

我的测试结果如下:

Enter hours work : 3
Enter pay rate: 2
You have entered the wrong pay rate. Please try again !!!
Enter pay rate: 5

Do you have any employee to process (Y/N)
y

Enter hours work : 2
Enter pay rate: 5

Do you have any employee to process (Y/N)
z
Please use Y or N only.

Do you have any employee to process (Y/N)
N

Calculation is doing. Please wait...