下面是描述:
请编写一个管理停车费的Java程序。每小时3美元。
这是输出的一个例子:
Please enter today's total parked cars: -3
The number you entered is less than 0, please re-enter: 0
The number you entered is less than 0, please re-enter: 8
Please enter your 1st car's parking hours: -1
The parking hours entered is incorrect, please re-enter: 25
The parking hours entered is incorrect, please re-enter: 30
The parking hours entered is incorrect, please re-enter: 24
Please enter your 2nd car's parking hours: 2
Please enter your 3rd car's parking hours: 5
Please enter your 4th car's parking hours: 9
Please enter your 5th car's parking hours: 4
Please enter your 6th car's parking hours: 3
----today's parking invoice----
ID. Amount Subtotal
1 _______30 _______ 30 *coupon
2 _______6 _______ 36
3 _______ 12_______ 48
4 _______ 3 _______ 51
5 _______30 _______ 81 *coupon
Today's total:$81这是我的代码:
import java.util.Scanner;
public class Tryagainparking {
public static void main(String[] args)
{
Scanner keyboard=new Scanner(System.in);
System.out.print("Total parked cars:");
int NUMBER_OF_CARS = keyboard.nextInt();
while (NUMBER_OF_CARS<=0)
{
System.out.print("Cars has to >0,please re-entere:");
int NUMBER_OF_CARS2 = keyboard.nextInt();
if (NUMBER_OF_CARS2>0)
break;
}
double hoursParked=0;
double EveryAmount=0;
int totalincome=0;
int counter=1;
for (int count=1;count<=NUMBER_OF_CARS;count++)
{
System.out.printf("this is the %d data:",count);
hoursParked=keyboard.nextInt();
while(hoursParked<=0)
{
System.out.printf("parking hour is incorrect, please re-eneter");
int hoursParked2=keyboard.nextInt();
}
while(hoursParked>=24)
{
System.out.printf("parking hour is incorrect, please re-eneter");
int hoursParked2=keyboard.nextInt();
}
EveryAmount=(hoursParked)*30;
if (EveryAmount>=300)
EveryAmount=300;
totalincome+=EveryAmount;
}
System.out.print("----invoice------\n");
System.out.print("ID Amount Subtotal");
System.out.printf("%s",counter);
}
}发布于 2023-06-03 14:42:52
这个测试看上去是错误的:
while(hoursParked<=0) { System.out.printf("parking hour is incorrect, please re-eneter"); int hoursParked2=keyboard.nextInt(); } while(hoursParked>=24) { System.out.printf("parking hour is incorrect, please re-eneter"); int hoursParked2=keyboard.nextInt(); }
在第二个循环中,我们可以输入一个负值,这将被接受。
我们希望替换我们之前读到的值,而不是扔掉它(当我们读取汽车数量时,我们似乎遇到了同样的问题)。
我们需要一个循环来重复,直到满足所有条件:
while (hoursParked<=0 || hoursParked>=24)
{
System.out.printf("parking hour is incorrect, please re-enter");
hoursParked = keyboard.nextInt();
}https://codereview.stackexchange.com/questions/285320
复制相似问题