JAVA do-while 开关打印菜单两次显示错误

JAVA Do-while switch prints menu twice display error

这是我的第一个问题!

我的 do-while 菜单循环没有正确重复。

似乎在用户将信息输入选项 A 或 B 后每次重复时都会在开关中打印默认选项(不是 X 'exiting'。而不是返回主菜单默认开关也与另一个主菜单一起打印...

好像收到了无效的输入,但是没有给出输入!

这是我的代码:

import java.util.Scanner;

public class Part3Final
{

      static double rateM = 1.40;
      static double rateC = 2.40;
      static double rateLCV = 3.80;
      static double rateHCV = 7.20;

      static String M = "M";
      static String C= ("C");
      static String LCV ="LCV";
      static String HCV = ("HCV");

      static String Peak = "Peak";
      static String offPeak = "Off-Peak";
      static String Night = "Night"   ;      

      public static void main(String[] args)
      {

         Scanner sc = new Scanner(System.in);

         System.out.print("\nEnter Vehicle Type (M, C, LCV, HCV):");
         String vehicleType = sc.nextLine();

         if (vehicleType.equals(HCV))
            System.out.print("Enter Trip Time (Peak, Off-Peak or Night):");
            String tripTime = sc.nextLine();     

         String choiceEntry;

         String tripDetails="";
         String tripList= "";

         int sectors;

      // Adjusted rate calculation
         double adjustedHCVNight = (7.2 - (rateHCV*0.3));
         double adjustedHCVPeak = ((rateHCV*0.4)+7.2);
      // Variables to alter later
         double adjustedSectorRate = 100;
         double tripRate = 0;
         double tripCharge= 0;
         double totalCharge=0;

         String breakList = "";
         String breakReport = "";

         double totalBreakCharge = 0;

       //b  char choiceEntryCh = '[=11=]';

         do   
         {
            System.out.println("\nToll Road Data Entry Menu");
            System.out.println("-----------------------------------------");
            System.out.println("\nA - First Option");
            System.out.println("B - Second Option");
            System.out.println("X - Exit");

            System.out.print("\nEnter your selection: ");
            choiceEntry = sc.nextLine();
            choiceEntry = choiceEntry.toUpperCase(); 

            if (choiceEntry.length() != 1)
               System.out.println("Response must be a single character!");
            else

               switch (choiceEntry)
               {
               // equivalent of doing if (selection == 'A' || selection == 'a')
                  case "A":
                  case "a":
                  { 
                     System.out.print("Enter Trip Date:");
                     String tripDate = sc.nextLine();
                     System.out.print("Enter Entry Point:");
                     int entryPoint = sc.nextInt();
                     System.out.print("Enter Exit Point:");
                     int exitPoint = sc.nextInt();

                     sectors= Math.abs(exitPoint-entryPoint);

                     if (tripTime.equals(Peak))
                           tripRate=(adjustedHCVPeak);             
                     if (tripTime.equals(Night))
                           tripRate= (adjustedHCVNight) ;
                     if (tripTime.equals(offPeak))
                           tripRate= (rateHCV);

                     if  (vehicleType.equals(M))
                           tripRate = rateM;
                     if (vehicleType.equals(C))
                           tripRate = rateC;
                     if (vehicleType.equals(LCV))
                           tripRate = rateLCV;

                     tripCharge= (tripRate * sectors);

                     tripDetails = String.format("%s %s %s %d %s %d %s %.2f %s %.2f %s", "- Trip on", tripDate, "from sector", entryPoint, "to sector", exitPoint, "at rate", tripRate, "(toll charge:", tripCharge,")");

                     tripList= (tripList + "\n" + tripDetails);

                     totalCharge= (totalCharge + tripCharge);
                     }
                     break;

                  case "B":
                  case "b":
                  {
                     System.out.print("\nEnter Breakdown Incident Date:");
                     String incidentDate = sc.nextLine();
                     System.out.print("Enter sector breakdown occured in:");
                     int breakdownPoint = sc.nextInt();
                     System.out.print("Enter vehicle recovery cost:");
                     double recoveryCost = sc.nextDouble();

                     breakReport = String.format("%s %s %s %d %s %.2f %s", "- Breakdown on", incidentDate, "in sector", breakdownPoint, "(recovery cost:", recoveryCost,")");

                     breakList= (breakList + "\n" + breakReport);

                     totalBreakCharge= (totalBreakCharge + recoveryCost);

                  }
                     break;

                  case "X":
                  case "x":
                     System.out.println("Exiting data entry menu...");
                     break;

                  default:
                     System.out.println("Error - invalid selection!");
                     break;


                  }     

          }while (!choiceEntry.equalsIgnoreCase("X"));
            //if (choiceEntry.equals("X"))
              //a System.out.println("Exiting data entry menu...");


         System.out.printf("\n%-70s %s\n", "Toll Charges:\n", tripList);

         System.out.printf("\n%s %.2f %s", "(Toll charge total: $",totalCharge,")");

         System.out.printf("\n%-70s %s\n", "\nBreakdown Charges:\n", breakList);

         System.out.printf("\n%s %.2f %s", "(Breakdown charge total: $",totalBreakCharge,")");

         double totalInvoice= (totalCharge + totalBreakCharge);

         System.out.printf("\n%-70s %.2f\n", "\nToll Invoice Total", totalInvoice);


         }


      }

这是我打印出来的 BOLDED 是错误:

输入车辆类型(M、C、LCV、HCV):HCV 输入行程时间(高峰、非高峰或夜间):夜间

收费公路数据输入菜单

A - 第一个选项 B - 第二个选项 X - 退出

输入您的选择:A 输入行程 Date:6 输入条目 Point:5 进入退出Point:3

收费公路数据输入菜单 A - 第一个选项 B - 第二个选项 X - 退出 输入您的选择:回复必须是单个字符!

请注意未输入回复

收费公路数据输入菜单

A - 第一个选项 B - 第二个选项 X - 退出

输入您的选择:一个 输入行程 Date:5/4/2017 输入条目 Point:5 进入退出Point:2

收费公路数据输入菜单 A - 第一个选项 B - 第二个选项 X - 退出 输入您的选择:回复必须是单个字符!

请注意未输入回复

收费公路数据输入菜单

A - 第一个选项 B - 第二个选项 X - 退出

输入您的选择:x 正在退出数据输入菜单...

通行费:

(通行费总计:25.20 美元)

故障费用:

(故障费用总计:0.00 美元)

通行费发票合计 25.20

谁能解释一下为什么会这样!

谢谢!

问题可能出在 sc.nextInt() 调用之后的 sc.nextLine() 调用中。由于 sc.nextInt() 不使用 \n 换行符,这可能就是您看到 Response must be a single character! 错误的原因。看这里:Scanner is skipping nextLine() after using next(), nextInt() or other nextFoo()?