while循环,切换语句

时间:2014-10-26 22:16:32

标签: java while-loop switch-statement

当我进入" p"我遇到了问题。在它结束之后,它再次重复我的while循环的顶部部分,最后用户必须输入一些东西,用于输入"变量。问题是用户永远不会被提示输入任何内容以便输入"然后它再次循环,重新打印之前刚刚打印的所有信息。围绕它的第二次确实允许用户输入内容以输入"。这只发生在我运行" p"情况下。

有人请帮帮我!非常感谢!

package PayandGoParking;

/**
 * Purpose: Input:
 *
 * Processing done:
 *
 * Output:
 *
 * @author (your name)
 * @version (a version number or a date)
 */
import java.util.Scanner;
import java.util.*;
import java.io.*;
import java.text.DecimalFormat;

public class Assignment2 {

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

  Calendar now = Calendar.getInstance();
  Scanner k = new Scanner(System.in);
  DecimalFormat dm = new DecimalFormat("00");

  String input = " ",
    cardNumber,
    x = "x";
  double anyKey;

  double currentTime,
    currentCharge = 0,
    securityCode,
    totalCharge = 0;

  Calendar paidTill;

  int min = 0;

  while (input != x) {
   System.out.println("Welcome to Pay and Go parking");
   System.out.println("+ to add 30 minutes ($1.00 charge)");
   System.out.println("p to print the ticket");
   System.out.println("r to restart");
   System.out.println("x to exit the program");
   System.out.print("Please enter your selection: ");
   input = k.nextLine();
   switch (input) {
    case "+":
     min += 30;
     currentCharge += 1;
     paidTill = (Calendar) now.clone();
     paidTill.add(Calendar.MINUTE, min);
     System.out.println("================================================");
     System.out.printf("%s%s%s%s%s%.2s%s%.2s\n", 
       "Current Time: ", now.get(Calendar.MONTH),
       "-", now.get(Calendar.DAY_OF_MONTH), " ", now.get(Calendar.HOUR_OF_DAY), ":", 
       dm.format(now.get(Calendar.MINUTE)));
     System.out.printf("%s%s%s%s%s%.2s%s%.2s\n", 
       "Paid Till: ", paidTill.get(Calendar.MONTH),
       "-", paidTill.get(Calendar.DAY_OF_MONTH),
       " ", paidTill.get(Calendar.HOUR_OF_DAY), ":",
       dm.format(paidTill.get(Calendar.MINUTE)));
     System.out.printf("%s%.2f\n", "Current Charge $", currentCharge);
     System.out.println("================================================");
     break;

    case "p":
     System.out.print("Enter Credit Card Number: ");
     cardNumber = k.nextLine();
     System.out.print("Enter 3 digit security code from back of Credit Card: ");
     securityCode = k.nextDouble();
     System.out.println("=================================================");
     System.out.println("Pay and Go Parking");
     System.out.println("Lot Location: 123 Main St,");
     System.out.println("Honolulu, Hawaii");
     System.out.println("-----------------------------");
     totalCharge += currentCharge;
     paidTill = (Calendar) now.clone();
     paidTill.add(Calendar.MINUTE, min);
     System.out.printf("%s%s%s%s%s%.2s%s%.2s\n", "Current Time: ", 
       now.get(Calendar.MONTH), "-",
       now.get(Calendar.DAY_OF_MONTH), " ", now.get(Calendar.HOUR_OF_DAY), 
       ":", dm.format(now.get(Calendar.MINUTE)));
     System.out.printf("%s%s%s%s%s%.2s%s%.2s\n", 
       "Paid Till: ", paidTill.get(Calendar.MONTH),
       "-", paidTill.get(Calendar.DAY_OF_MONTH),
       " ", paidTill.get(Calendar.HOUR_OF_DAY), ":",
       dm.format(paidTill.get(Calendar.MINUTE)));
     System.out.printf("%s%.2f\n", "Total charged to Credit Card $", currentCharge);
     System.out.println("==========================================");
     System.out.println("Tear off receipt and place on dash");
     System.out.print("Press any key to continue");
     anyKey = k.nextDouble();
     System.out.println("================================================");
     paidTill = (Calendar) now.clone();
     System.out.printf("%s%s%s%s%s%.2s%s%.2s\n", "Current Time: ", 
       now.get(Calendar.MONTH), 
       "-", now.get(Calendar.DAY_OF_MONTH), " ", 
       now.get(Calendar.HOUR_OF_DAY), ":", dm.format(now.get(Calendar.MINUTE)));
     System.out.printf("%s%s%s%s%s%.2s%s%.2s\n", 
       "Paid Till: ", paidTill.get(Calendar.MONTH),
       "-", paidTill.get(Calendar.DAY_OF_MONTH),
       " ", paidTill.get(Calendar.HOUR_OF_DAY), ":",
       dm.format(paidTill.get(Calendar.MINUTE)));
     currentCharge = 0;
     min = 0;
     System.out.printf("%s%.2f\n", "Current Charge $", currentCharge);
     System.out.println("================================================");
     break;

    case "r":
     currentCharge = 0;
     min = 0;
     paidTill = (Calendar) now.clone();
     System.out.println("================================================");
     System.out.printf("%s%s%s%s%s%.2s%s%.2s\n",
       "Current Time: ", now.get(Calendar.MONTH),
       "-", now.get(Calendar.DAY_OF_MONTH), " ",
       now.get(Calendar.HOUR_OF_DAY), ":",
       dm.format(now.get(Calendar.MINUTE)));
     System.out.printf("%s%s%s%s%s%.2s%s%.2s\n", 
       "Paid Till: ", paidTill.get(Calendar.MONTH),
       "-", paidTill.get(Calendar.DAY_OF_MONTH), 
       " ", paidTill.get(Calendar.HOUR_OF_DAY),
       ":", dm.format(paidTill.get(Calendar.MINUTE)));
     System.out.printf("%s%.2f\n", "Current Charge $", currentCharge);
     System.out.println("================================================");
     break;

    case "x":
     input = x;
     break;
   }
  }
  System.out.println("\nTotals for Pay and Go Parking");
  System.out.println("========================");
  System.out.printf("%s%12s%7.2f\n", "Totals", "$", totalCharge);

 }

}

2 个答案:

答案 0 :(得分:2)

您应该解决的第一个问题是第44行:

while (input != x)

应该是:

while (!input.equals(x)) 

在比较两个字符串时使用==或!=不会检查它们是否相同 字符/单词,但如果它们等于相同的字符串含义指向内存中的相同位置。

我无法运行该程序来测试它,因为我没有你的软件包" PayandGoParking"。 并且您的代码太长而无法通读以查找更多问题。 修复此问题后,尝试使代码更简洁。

答案 1 :(得分:0)

这很可能是因为您使用System.out.print而不进行刷新。刷新意味着强制将System.out内部使用的输出缓冲区发送到监视器。只有println才能保证将您打印的内容发送到显示器。除非字符串中的某处有换行符,否则不会使用常规System.print。试试

 System.out.print("Please enter your selection: ");
 System.out.flush();

实际上尝试在每次尝试收集用户输入之前添加System.out.flush()。 或者在您正在打印的字符串中添加一个新行

 System.out.print("Please enter your selection: \n");