2016-11-20 48 views
-3

我正在創建一些代碼,用於向用戶顯示服務及其相應價格的array包含陣列和While循環的用戶輸入

每當用戶選擇的服務,我要顯示給用戶的服務的價格...

如果用戶希望進行用於將多個服務,我希望計算機問他們,直到他們另有說法。

此外,我需要使用累加器來添加用戶請求的服務的價格。

到目前爲止,這是我所:

import java.util.Scanner; 
public class Assign3 
{ 
    public static void carMake(String bmw) 
    { 
     Scanner input = new Scanner(System.in); 

     //declare and intialize parallel arrays, Services and Prices and display them to the user 
     String[] services = {"Oil Change" , "Tire Rotation", "Air Filter", "Fluid Check"}; //intialize list of services 
     double[]price = {39.99, 49.99, 19.99, 10.99}; //initialize corresponding price for services 
     for(int i= 0; i < services.length; i++) 
     { 
      System.out.print(services[i]+ "....");  
      System.out.print(price[i] + "\t"); 


     } 

     System.out.println("What service do you want done?: "); 
     String service= input.nextLine(); 




    } 
     public static void main(String[]args) 
     { 
      String bmw = "fancy car"; 
      carMake(bmw); 


     } 
} 
+0

你的意思是類似這樣的東西:http://stackoverflow.com/questions/40689994/printing-all-the-items/ – DevilsHnd

+0

,你也一樣例如,選項的狀態號碼油更換爲1或用戶是否必須鍵入「換油」?你將如何記住用戶的選擇? –

+0

用戶將不得不輸入換油信息。 我希望系統使用累加器(i ++)來添加用戶想要完成的每個服務,並在他們選擇了他們想要的所有服務時給它們一個通用服務。 –

回答

2

雖然我沒有提交完整的程序,我可以給你,你需要完成這個項目的所有信息。首先,你必須決定如何整合服務和價格之間的聯繫。

更好的方法而不是兩個數組將創建一個「服務」對象。例如:

public class Service { 
    // Variables to use 
    String name = ""; 
    double price = 0.0; 

    // Constructor of the 'Service' class 
    public Service(String inputName, double inputPrice) { 
      name = inputName; 
      price = inputPrice; 
    } 
} 

這將允許你再撥打電話:

// Create the oilChange service 
Service oilChange = new Service("Oil Change", 39.99); 

// Get the oilChange values: 
System.out.println(oilChange.name); // Displays 'Oil Change' 
System.out.println(oilChange.price); // Displays '39.99' 

從那裏,你必須確定是否要使用for-loopwhile- loop。 A for-loop要求您知道循環將運行多少次。但是,就你的例子而言,這是你不會知道的。因此,您將不得不使用while-loopwhile-loop爲您的程序的示例如下所示:

double totalPrice = 0.0; 
boolean userIsDone = false; 
Service listOfServices = [] // Contains created service objects from above 
while (userIsDone == false) { 
    System.out.println("What service do you want done? (Type 'quit' to exit): "); 
    String service= input.nextLine(); 
    if (input.equalsIgnoreCase("quit")) { 
      userIsDone = true; 
    } else { 
      /* 
      1.) Loop through the 'listOfServices' array finding 
       a service with the name that matches user input 
      2.) Then retrieve the price of that service and add 
       it to the 'totalPrice' counter 
      */ 
    } 
} 
System.out.println(totalPrice); // Updated total price! 

希望這有助於您!

0

因此,如果我的理解正確,您希望我們創建一些代碼,直到用戶想要退出該程序爲止。問題不明確,你告訴我們你想做什麼,而不是你希望我們做什麼,如果我錯誤地解釋被問到的問題,請原諒。這是我的答案,我想你問的問題是:

import java.util.Scanner; 
public class Assign3 
{ 
    public static void carMake(String bmw) 
    { 
     Scanner input = new Scanner(System.in); 

     //declare and intialize parallel arrays, Services and Prices and display them to the user 
     String[] services = {"Oil Change" , "Tire Rotation", "Air Filter", "Fluid Check"}; //intialize list of services 
     double[]price = {39.99, 49.99, 19.99, 10.99}; //initialize corresponding price for services 
     for(int i= 0; i < services.length; i++) 
     { 
      System.out.print(services[i]+ "....");  
      System.out.print(price[i] + "\t"); 


     } 

     do 
     { 
      System.out.println("What service do you want done?: \n"+ 
           "1. Oil Change\n"+ 
           "2. Tire Rotation\n"+ 
           "3. Etc.\n"+ 
           "4. Etc.\n"+ 
           "5. Exit Program"); 
      int service= input.nextInt(); 

      System.out.println("\nThe price for that service will be: "+ price[service]); 
     } 
     while(input.hasNextInt()&&service!=5) 


    } 
     public static void main(String[]args) 
     { 
      String bmw = "fancy car"; 
      carMake(bmw); 


     } 
}