所以我需要做的是计算周一至周五从0900到1600的租赁时间并存储
我已经尝试删除一天的计算,但是仍然无法以正确的格式进行编译。
public class Time {
// Variables
static Scanner kbinput = new Scanner(System.in);
private static boolean error = false;
private static String daysoftheweek; // Monday, Tuesday, Wednesday, Thursday, Friday, Saturday, Sunday;
DateFormat sdf = new SimpleDateFormat("HH");
// Keyboard for user input
private int time;
public static void main(String[] args) throws ParseException {
System.out.println("Please enter day of your rental");
daysoftheweek = kbinput.nextLine();
System.out.println("Please enter day of your return");
do {
daysoftheweek = kbinput.nextLine();
switch (daysoftheweek.toUpperCase()) {
case "Monday":
error = false;
break;
case "Tuesday":
error = false;
break;
case "Wednesday":
error = false;
break;
case "Thursday":
error = false;
break;
case "Friday":
break;
case "Saturday":
default:
case "Sunday":
}
} while (error);
System.out.println("Please enter your start time (hh) ");
String time = kbinput.nextLine();
System.out.println();
System.out.print("Enter finish time (hh) ");
String time2 = kbinput.nextLine();
DateFormat sdf = new SimpleDateFormat("hh");
Date d1 = sdf.parse(time);
System.out.println("Rental Time: " + sdf.format(d1));
}
}
答案 0 :(得分:0)
Java中的字符串比较区分大小写。在该行中:
switch (daysoftheweek.toUpperCase()) {
您将用户的输入转换为全部大写的字符串,即“星期一”变为“星期一”。这永远不会匹配switch块中的任何情况。
一个短期解决方案是将所有案例替换为其大写版本。一种更可靠的方法是在一周中的几天内使用内置的Java enumeration。