我试图让我的代码成功,以便7以上的数字花费更多,从8到24的时间,0到7和2法郎之间的每小时花费1法郎。现在我有这个:
int start = user_input.nextInt();
int finish = user_input.nextInt();
int hours = finish - start;
int hours2 = finish - start;
int francs1 = 1 * hours;
int francs2 = 2 * hours;
if (start > 0 && finish < 7){
System.out.println(hours + " hour(s) at a cost of " + francs1 + " francs");
}else{
if (start > 7 && finish < 24){
System.out.println(hours2 + " hour(s) at a cost of " + francs2 + " francs");
}else{
if (start < 7 && finish > 7){
Start是用户输入的第一个值,finish是第二个值。
答案 0 :(得分:2)
你在找这样的东西吗?
int start = 8
int end = 10
int leftHandDiff = Math.min(7, end) - start
int rightHandDiff = end - Math.max(7, start)
int francs1 = (leftHandDiff > 0 ? leftHandDiff : 0) * 1
int francs2 = (rightHandDiff > 0 ? rightHandDiff : 0) * 2
当然,您必须包含您的验证,以便小时不低于0或大于24,并且该开始不是在结束之后。