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

时间:2017-10-09 19:51:10

标签: java menu switch-statement default do-while

这是我的第一个问题!

我的do-while菜单循环不能正确重复。

在用户将选项A或B输入信息后(而非X'退出'时,似乎每次重复打开交换机中的默认选项。而不是返回主菜单,也会打印默认开关以及另一个主菜单......

就像它收到了无效的输入但没有给出任何输入!

这是我的代码:

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 = '\0';

         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 输入旅行日期:6 输入入口点:5 输入退出点:3

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

注意没有响应输入

收费公路数据输入菜单

A - 第一选择 B - 第二选择 X - 退出

输入您的选择:a 输入旅行日期:2017年5月4日 输入入口点:5 输入退出点:2

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

注意没有响应输入

收费公路数据输入菜单

A - 第一选择 B - 第二选择 X - 退出

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

通行费:

  • 从第5区到第3区的6路旅行,费率为5.04(通行费:10.08)
  • 2017年5月4日旅行从第5区到第2区,费率为5.04(通行费:15.12)

(通行费总额:25.20美元)

细分费用:

(分解费用总额:0.00美元)

收费发票总计25.20

有人可以解释为什么会发生这种情况!

谢谢!

1 个答案:

答案 0 :(得分:0)

问题可能出在sc.nextLine()来电后的sc.nextInt()来电中。由于sc.nextInt()不会使用\n换行符,这可能是您看到响应必须是单个字符的原因!错误。见这里:Scanner is skipping nextLine() after using next(), nextInt() or other nextFoo()?