Java重复比较值与数组双精度并在错误输入后重复

当用户输入价值支付率时,它会检查价值支付率与支付率数组。如果正确,那么它会提示用户是否需要完成另一个过程。如果没有,它将去计算。


如果支付率错误,则显示错误并要求用户重新输入值。


我有问题,在用户按下“Y”后,它会显示“错误的支付率,请重试”。


public static void main(String[] args) {


    //Scanner

    Scanner read = new Scanner(System.in);


    //Array of payRate Default

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


    double payRateEntered;

    boolean isPayRate = false;


    char anotherProcess;


    System.out.print("Enter hours work: ");

    int hoursWork = read.nextInt();


    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


                        break;

                    default:

                        //If wrong input

                        System.out.println("Please use Y or N only");

                        break;

                }

            } else {

                isPayRate = false;

            }

        }

        System.out.println("You have entered the wrong pay rate. Please try again");

    } while (!isPayRate);

}

结果:

http://img1.mukewang.com/6386ca60000162bb04550270.jpg

胡说叔叔
浏览 161回答 2
2回答

隔江千里

以下代码对我来说很好用:package mm.com.java.so.test;import java.io.BufferedReader;import java.io.IOException;import java.io.InputStreamReader;public class JavaRepeat {&nbsp; &nbsp; public static void main(String[] args) throws IOException {&nbsp; &nbsp; &nbsp; &nbsp; BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));&nbsp; &nbsp; &nbsp; &nbsp; double[] defaultPayRates = { 3.50, 4.00, 4.50, 4.75, 5.00, 5.25, 5.50, 5.75, 6.00 };&nbsp; &nbsp; &nbsp; &nbsp; double payRate;&nbsp; &nbsp; &nbsp; &nbsp; boolean isValid;&nbsp; &nbsp; &nbsp; &nbsp; boolean isContinue;&nbsp; &nbsp; &nbsp; &nbsp; String next;&nbsp; &nbsp; &nbsp; &nbsp; int workingHour;&nbsp; &nbsp; &nbsp; &nbsp; do {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; isValid = false;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; isContinue = false;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.print("\nEnter hours work : ");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; workingHour = Integer.parseInt(reader.readLine());&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; do {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.print("Enter pay rate: ");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; payRate = Double.parseDouble(reader.readLine());&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; for (int i = 0; i < defaultPayRates.length; i++) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (defaultPayRates[i] == payRate) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; isValid = true;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // TODO : make calculation here.&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; break;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (!isValid) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println("You have entered the wrong pay rate. Please try again !!!");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; } while (!isValid);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; do {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; isValid = true;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println("\nDo you have any employee to process (Y/N)");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; next = reader.readLine();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; switch (next.toLowerCase()) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; case "y":&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; isContinue = true;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; break;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; case "n":&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; isContinue = false;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; break;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; default:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; isValid = false;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println("Please use Y or N only.");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; break;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; } while (!isValid);&nbsp; &nbsp; &nbsp; &nbsp; } while (isContinue);&nbsp; &nbsp; &nbsp; &nbsp; // TODO : print out calculation here.&nbsp; &nbsp; &nbsp; &nbsp; System.out.println("\nCalculation is doing. Please wait...");&nbsp; &nbsp; }}我的测试结果如下:Enter hours work : 3Enter pay rate: 2You have entered the wrong pay rate. Please try again !!!Enter pay rate: 5Do you have any employee to process (Y/N)yEnter hours work : 2Enter pay rate: 5Do you have any employee to process (Y/N)zPlease use Y or N only.Do you have any employee to process (Y/N)NCalculation is doing. Please wait...

梵蒂冈之花

仅当is时,您才应该执行该System.out.println("You have entered the wrong pay rate. Please try again");行。 isPayRatefalseboolean finished = false;do {&nbsp; &nbsp; System.out.print("Enter pay rate: ");&nbsp; &nbsp; payRateEntered = read.nextDouble();&nbsp; &nbsp; for (int i = 0; i < payRateDefault.length; i++) {&nbsp; &nbsp; &nbsp; &nbsp; if (payRateDefault[i] == payRateEntered) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //If the payRate is true with array payRateDefault, proceed to ask if you have another employee&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println("Do you have any employee to process (Y/N)");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; anotherProcess = read.next().charAt(0);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; isPayRate = true;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //Check if Y or N&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; switch (anotherProcess) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; case 'Y':&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //Proceed back to prompt user to enter pay rate&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; break;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; case 'N':&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //Proceed to calculation&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; finished = true;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; break;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; default:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //If wrong input&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println("Please use Y or N only");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; break;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; break;&nbsp; &nbsp; &nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; isPayRate = false;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; if (!isPayRate) {&nbsp; &nbsp; &nbsp; &nbsp; System.out.println("You have entered the wrong pay rate. Please try again");&nbsp; &nbsp; }} while (!finished);
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Go