简体   繁体   English

while循环,switch语句

[英]while loop, switch statement

I'm having a problem when I enter the "p" case, after it's finished it repeats the top part of my while loop again where at the end the user must put something in for "input" variable. 输入“ p”大小写时出现问题,完成后它再次重复我的while循环的顶部,最终用户必须在其中输入“ input”变量。 The problem is the user never gets prompted to put anything in for "input" and then it loops again, re-printing all of the information it previously just printed. 问题是用户永远不会被提示输入任何内容作为“输入”,然后再次循环,重新打印以前刚刚打印的所有信息。 The second time around it does allow the user to put something in for "input". 围绕它的第二次确实允许用户放入一些东西作为“输入”。 This only happens after I run the "p" case. 这仅在我运行“ p”情况后发生。

Someone please help me! 有人请帮助我! Will be much appreciated! 将不胜感激!

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);

 }

}

One of the first issues you should fix is on line 44: 您应该解决的第一个问题是第44行:

while (input != x)

Should be: 应该:

while (!input.equals(x)) 

Using == or != when comparing two Strings does not check if they are equal to the same characters/words but if they are equal to the same String meaning point to the same location in memory. 比较两个字符串时,使用==或!=不会检查它们是否等于相同的字符/单词,但不等于相同的字符串/含义意味着指向内存中的同一位置。

I can't run the program to test it because I don't have your package "PayandGoParking". 我没有运行程序来测试它,因为我没有您的软件包“ PayandGoParking”。 And your code is too long to read through to look for more problems. 而且您的代码太长,无法通读以查找更多问题。 Try to make the code that is giving a problem more concise after you fix this. 解决此问题后,请尝试使出现问题的代码更简洁。

This is likely because you're using System.out.print without flushing. 这可能是因为您使用的是System.out.print而没有刷新。 Flushing means force sending the output buffer that System.out internally uses to the monitor. 刷新意味着强制将System.out内部使用的输出缓冲区发送到监视器。 Only println is guaranteed to send what you printed to the monitor. 仅保证println将您打印的内容发送到显示器。 Regular System.print does not unless there is a newline character somewhere in your string. 常规System.print不会,除非您的字符串中某处有换行符。 Try 尝试

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

In fact try adding System.out.flush() before every attempt to collect user input. 实际上,尝试在每次尝试收集用户输入之前添加System.out.flush()。 Or add a new line in the string you're printing 或者在您要打印的字符串中添加新行

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM