Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/331.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/macos/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java while循环,switch语句_Java_While Loop_Switch Statement - Fatal编程技术网

Java while循环,switch语句

Java while循环,switch语句,java,while-loop,switch-statement,Java,While Loop,Switch Statement,当我输入“p”时,我遇到了一个问题,在它完成后,它再次重复while循环的顶部,在那里,用户必须为“input”变量输入一些内容。问题是,用户从未被提示输入任何内容,然后它再次循环,重新打印之前刚刚打印的所有信息。第二次是允许用户输入一些东西。这只有在我运行“p”案例之后才会发生 有人请帮帮我!非常感谢 package PayandGoParking; /** * Purpose: Input: * * Processing done: * * Output: * * @auth

当我输入“p”时,我遇到了一个问题,在它完成后,它再次重复while循环的顶部,在那里,用户必须为“input”变量输入一些内容。问题是,用户从未被提示输入任何内容,然后它再次循环,重新打印之前刚刚打印的所有信息。第二次是允许用户输入一些东西。这只有在我运行“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);

 }

}

您应该首先解决的问题之一是第44行:

while (input != x)
应该是:

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

我无法运行程序来测试它,因为我没有你的软件包“PayandGoParking”。 您的代码太长,无法通读,无法查找更多问题。
解决此问题后,请尝试使出现问题的代码更简洁。

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

 System.out.print("Please enter your selection: ");
 System.out.flush();
实际上,在每次尝试收集用户输入之前,请尝试添加System.out.flush()。 或者在正在打印的字符串中添加新行

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

请首先帮助我们将代码示例缩减到可管理的大小,以说明问题。请向我们展示示例-此外,您可能会在编写新代码段时发现问题。