I am doing this assignment for my CSCI 1301 class and i'm kinda stuck. The assignment is to write a program that will provide a list of services to users and allow them to choose any or all services they would like and display the final price. Here's my code so far,
我正在为我的CSCI 1301课做这个任务,我有点卡住了。任务是编写一个程序,为用户提供服务列表,并允许他们选择他们想要的任何或所有服务并显示最终价格。到目前为止,这是我的代码,
public static void carMaintenance()
{
Scanner sc = new Scanner(System.in);
String makeOfCar;
System.out.println("What's the make of your car?");
makeOfCar = sc.next();
String[] services = {"Oil Change", "Tire Rotation", "Air Filter", "Fluid Check"};
double[] prices = {39.99, 49.99, 19.99, 10.99};
System.out.println("What services whould you like for your "+makeOfCar+"?");
System.out.println(services[0]+", "+services[1]+", "+services[2]+", "+services[3]+".");
}
Where i'm stuck at is how would I go about allowing the user to request as many services they want?(Logically speaking, they can only request up to 4 services)
我坚持的地方是如何让用户请求他们想要的尽可能多的服务?(从逻辑上讲,他们最多只能请求4项服务)
I figured I could use another array and put it in a "do-while" loop to achieve this but then, once I check it against the "services" array how would I assign a price to each service the user requested so that I can calculate the total price for all requested services?
我想我可以使用另一个数组并将其置于“do-while”循环中以实现此目的但是,一旦我针对“服务”数组检查它,我将如何为用户请求的每个服务分配价格以便我可以计算所有要求服务的总价?
any insight and help is greatly appreciated!
非常感谢任何见解和帮助!
1 个解决方案
#1
0
You could keep track of the sum in an extra variable. So your idea with the extra array to check for duplicates would still work. Check if the service is already chosen and if not, add the chosen index to the sum.
您可以在额外变量中跟踪总和。因此,使用额外数组检查重复项的想法仍然可行。检查是否已选择服务,如果已选择,则将所选索引添加到总和中。
public static void carMaintenance() {
Scanner sc = new Scanner(System.in);
String makeOfCar;
System.out.println("What's the make of your car?");
makeOfCar = sc.nextLine();
String[] services = {"Oil Change", "Tire Rotation", "Air Filter", "Fluid Check"};
double[] prices = {39.99, 49.99, 19.99, 10.99};
double sum = 0;
System.out.println("What services whould you like for your " + makeOfCar + "?");
System.out.println(services[0] + ", " + services[1] + ", " + services[2] + ", " + services[3] + ".");
String choice;
//This array simply tracks true or false for chosen/not chosen. boolean arrays are initialized with all values false.
boolean[] chosenServices = new boolean[services.length]; //Can only be as many elements as there are services. This way you don't have to change it when you add another service
do {
choice = sc.nextLine();
int choiceIndex = getIndex(services, choice);
if (choiceIndex < 0) break; //Choice doesn't exist. You will have to refine that and add messages
if (!chosenServices[choiceIndex]) { //service not yet chosen
chosenServices[choiceIndex] = true;
sum += prices[choiceIndex];
} else {
System.out.println("Was already chosen!");
}
} while (!choice.toLowerCase().equals("exit")); //Or something similar
System.out.printf("%.2f", sum); //Price with 2 digits
}
public static int getIndex(String[] arr, String search) {
for (int i=0; i<arr.length; i++) {
if (search.equals(arr[i]))
return i;
}
return -1;
}
As mentioned in one of the comments, this is only a rough implementation. You might want to do this with index input from the user, you might want to check for false inputs more precise than I did here etc. but that should do what I think you mean.
正如其中一条评论中所提到的,这只是一个粗略的实现。您可能希望使用来自用户的索引输入执行此操作,您可能希望检查错误输入比我在此处更精确等等,但这应该做我认为您的意思。
#1
0
You could keep track of the sum in an extra variable. So your idea with the extra array to check for duplicates would still work. Check if the service is already chosen and if not, add the chosen index to the sum.
您可以在额外变量中跟踪总和。因此,使用额外数组检查重复项的想法仍然可行。检查是否已选择服务,如果已选择,则将所选索引添加到总和中。
public static void carMaintenance() {
Scanner sc = new Scanner(System.in);
String makeOfCar;
System.out.println("What's the make of your car?");
makeOfCar = sc.nextLine();
String[] services = {"Oil Change", "Tire Rotation", "Air Filter", "Fluid Check"};
double[] prices = {39.99, 49.99, 19.99, 10.99};
double sum = 0;
System.out.println("What services whould you like for your " + makeOfCar + "?");
System.out.println(services[0] + ", " + services[1] + ", " + services[2] + ", " + services[3] + ".");
String choice;
//This array simply tracks true or false for chosen/not chosen. boolean arrays are initialized with all values false.
boolean[] chosenServices = new boolean[services.length]; //Can only be as many elements as there are services. This way you don't have to change it when you add another service
do {
choice = sc.nextLine();
int choiceIndex = getIndex(services, choice);
if (choiceIndex < 0) break; //Choice doesn't exist. You will have to refine that and add messages
if (!chosenServices[choiceIndex]) { //service not yet chosen
chosenServices[choiceIndex] = true;
sum += prices[choiceIndex];
} else {
System.out.println("Was already chosen!");
}
} while (!choice.toLowerCase().equals("exit")); //Or something similar
System.out.printf("%.2f", sum); //Price with 2 digits
}
public static int getIndex(String[] arr, String search) {
for (int i=0; i<arr.length; i++) {
if (search.equals(arr[i]))
return i;
}
return -1;
}
As mentioned in one of the comments, this is only a rough implementation. You might want to do this with index input from the user, you might want to check for false inputs more precise than I did here etc. but that should do what I think you mean.
正如其中一条评论中所提到的,这只是一个粗略的实现。您可能希望使用来自用户的索引输入执行此操作,您可能希望检查错误输入比我在此处更精确等等,但这应该做我认为您的意思。