循环遍历服务数组,并询问用户需要哪些服务用于其<插入模型>



以下是我的说明:总的来说,询问用户他们拥有什么牌子的汽车。将此数据存储在字符串中。

  • 您的第一个方法应该使用make-vvia参数,并向用户显示以下问候语:Hello!我们很乐意今天为您的汽车提供服务!

  • 编写第二个名为carMaintenance的方法。它应该接受make via参数并将价格返回给Main。

o创建两个本地数组:服务和价格。§一个人会抓住这些绳子:换油、轮胎旋转、空气滤清器、检查液体§第二名将获得这些双打:39.99、49.99、19.99、10.99o循环浏览服务数组,询问用户需要哪些服务。请确保将价格与服务一起显示。使用价格数组,使用累加器来合计所有请求服务的价格。o将价格退还给Main。

  • 编写第三个方法名称finalPrice,它接受Main中的价格

o首先,劳动力价格加30%。o然后,询问这辆车是否是进口车。如果答案是肯定的,在价格上再加5%。o增加7%的销售税。o通过此方法向用户显示总价。

方法1已经完成,但是方法2我需要帮助循环,我想知道我是否可以得到一些帮助。因为它只是问我模型,然后问候我。

import java.util.Scanner;

公共类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; );
}
};

}

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();

目前还不清楚模型如何与您正在做的事情以及您打算如何使用它,但我把它留在了这里,就像它在您的原始代码中一样

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();

此外,您应该在某个地方创建一个常量(最终(变量,以指示您有多少服务和价格,并且您应该在循环迭代中引用该变量

最新更新