2016-11-20 8 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

私は完全なプログラムを提出していないよが、私はあなたがこのプロジェクトを完了するために必要なすべての情報を与えることができます。まず、サービスとその価格との間の接続を組み込む方法を決定する必要があります。

2つの配列ではなく、より良いアプローチは "Service"オブジェクトを作成することです。たとえば:

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を使用するかどうかを決定する必要があります。 for-loopでは、ループの実行回数を知る必要があります。しかし、あなたの例では、これはあなたが知らないものです。したがって、while-loopを使用する必要があります。プログラムのwhile-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); 


     } 
} 
関連する問題