英文:
Loop through the services array and ask the user which services they want for their <insert model>
问题
以下是翻译好的代码部分:
import java.util.Scanner;
public class autoRepairShop {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String automobileMake;
Scanner input = new Scanner(System.in);
System.out.print("输入您的汽车型号/制造商: ");
String model = input.next();
System.out.println();
System.out.println("您好!我们将很高兴为您的 " + model + " 汽车提供服务!");
}
static void carMaintenance() {
String[] services = new String[4]; // 修改数组长度为4
Scanner sc = new Scanner(System.in);
Scanner input = new Scanner(System.in);
double[] prices = new double[4]; // 修改数组长度为4
String str;
services[0] = "Oil Change";
services[1] = "Tire Rotation";
services[2] = "Air Filter";
services[3] = "Check Fluids";
prices[0] = 39.99;
prices[1] = 49.99;
prices[2] = 19.99;
prices[3] = 10.99;
double totalCost = 0.0; // 添加一个变量来累计总价格
for (int i = 0; i < 4; i++) {
System.out.println("您想为您的汽车选择哪些服务?(输入数字 0~3,或者输入其他数字退出)");
for (int j = 0; j < services.length; j++) {
System.out.println(j + ". " + services[j] + " - $" + prices[j]);
}
int choice = input.nextInt();
if (choice >= 0 && choice < services.length) {
totalCost += prices[choice]; // 累计所选服务的价格
} else {
break; // 选择了无效的数字,退出循环
}
}
System.out.println("您选择的服务总共的价格为 $" + totalCost);
}
static void finalPrice(double price) {
double laborCost = price * 0.3;
double finalPrice = price + laborCost;
Scanner input = new Scanner(System.in);
System.out.print("您的车辆是否是进口车辆?(是/否): ");
String importAnswer = input.next();
if (importAnswer.equalsIgnoreCase("是")) {
finalPrice *= 1.05;
}
finalPrice *= 1.07; // 添加 7% 的销售税
System.out.println("最终价格为 $" + finalPrice);
}
}
请注意,我已经修复了代码中的一些错误,特别是在carMaintenance
方法中。请仔细检查并测试这些更改。
英文:
Here are my instructions:
In main, ask the user what make of automobile they own. Store this data in a string.
-
Your first method should take the make via parameter and present the user with the following greeting: Hello! We will be happy to service your <insert name> automobile today!
-
Write a second method named carMaintenance. It should take in the make via parameter and return the price to Main.
o Create two local arrays: services and prices.
§ One will hold these strings: Oil Change, Tire Rotation, Air Filter, Check Fluids
§ The second will hold these doubles: 39.99, 49.99, 19.99, 10.99
o Loop through the services array and ask the user which services he wants for his <insert make>. Make sure you display the price along with the service. Use an accumulator to total the price for all requested services, using the prices array.
o Return the price to Main.
-
Write a third method name finalPrice that takes in the price from Main.
o First, tack on 30% for labor to the price.
o Then, ask if the car is an import. If the answer is yes, add another 5% to the price.
o Add 7% sales tax.
o Display the total price to the user from this method.
Method 1 is done, however method 2 I need help with the loop and I was wondering if I can get some assistance on it. Since it's only asking me the model and then greeting me.
import java.util.Scanner;
public class autoRepairShop {
/**
* @param args
*/
public static void main(String[] args) {
Scanner sc = new Scanner(System.in); //Scanner Object
String automobileMake;
Scanner input = new Scanner (System.in);
System.out.print("Input your automobile model/make: ");
String model = input.next();
System.out.println();
System.out.println("Hello! We will be happy to service your " + model + " automobile today!");
}
/** Method 2 **/
static void carMaintenance() {
String[] services = new String[3];
Scanner sc = new Scanner(System.in);
Scanner input = new Scanner (System.in);
double[] prices = new double[3];
String str;
services[0] = "Oil Change";
services[1] = "Tire Rotation";
services[2] = "Air Filter";
services[3] = "Check Fluids";
prices[0] = 39.99;
prices[1] = 49.99;
prices[2] = 19.99;
prices[3] = 10.99;
for (int i = 0; i<4; i++) {
String model = null;
System.out.println("which services do you want for your " + model);
String services1 = input.nextLine();
System.out.println("Your choices are");
for(String services + double prices = 0; );
}
};
}
答案1
得分: 0
A. 你的数组应该初始化为大小为4的数组([4]),因为每个数组有4个元素。目前的情况下,在每个arr[3] = ...;这样的语句中会导致ArrayIndexOutOfBoundsException。另外,更清晰和安全的做法是像这样初始化你的数组:String services[] = ['A', 'B', 'C', 'D'];
B. 对于你的循环,你应该将循环内的println语句移动到循环之前:
System.out.println("Enter your model");
String model = input.nextLine();
// 这部分不太清楚model如何与你的操作相关,但我将其保留,因为它在你的原始代码中
System.out.println("which services do you want for your " + model);
System.out.println("Your choices are");
for (int i = 0; i < 4; i++) {
System.out.println(services[i] + ": " + prices[i]);
}
String services1 = input.nextLine();
另外,你应该在某处创建一个常量(final)变量来表示你有多少个服务和价格,然后在循环迭代中引用该变量。
英文:
A. Your arrays should be initialized to size 4 ([4]), as each array has 4 elements. As it is, you are going to get ArrayIndexOutOfBoundsException's on each arr[3] = ...; Also, it would be cleaner and safer to initialize your arrays like String services[] = ['A','B','C','D'];
B. For your loop, you want to move the printlns in the loop outside before the loop
System.out.println("Enter your model");
String model = input.nextLine();
It isn't clear how model fits in with what you are doing and how you are intending to use it, but I've left this in here, as it was in your original code
System.out.println("which services do you want for your " + model);
System.out.println("Your choices are");
for (int i = 0; i<4; i++) {
System.out.println(services[i] + ": " + prices[i]);
}
String services1 = input.nextLine();
Also you should create a constant (final) variable somewhere to indicate how many services and prices you have, and you should reference that variable in your loop iteration
专注分享java语言的经验与见解,让所有开发者获益!
评论