java - How to calculate multiple user input (IF statements ) to return a total -


good morning/afternoon,

i'm having issue code works fine, aside exception handling course has not reached staged please omit these functions. setup variable used calculate total bill, when compiling keeps saying "floodsort" , "pipesort" not initialized.

i tried using separate if statements assign value of variable, perhaps not correct way. attempted full set of if statements written in comment under program. not return desired results. same scenario variables not initialized.

question: best way return customers total bill?

i extremely excited , grateful assistance on previous post, assistance or suggestions appreciated. i'm taking online classes because none offered in person best can get!

    import java.util.scanner;  /*author:    date:    program: write class "plumbers" handles emergency plumbing calls. company handles natural floods , burst pipes. if customer selects flood, program must prompt user determine amount of damage pricing. flood charging based on numbers of damaged rooms. 1 room costs $300.00, 2 rooms cost $500.00, , 3 or more rooms cost $750.00. pipe bursting based on number of pipes: 1 pipe costs $50.00, 2 pipes cost $70.00, , 3 or more pipes cost $100.00   */  public class plumbers   {     public static void main(string[] args)    {     //declarations         double floodsort;       double pipesort;       double totalcost;       int answer;       int floanswer;       int pipeanswer;       // declare flood room damage cost: 1 room costs $300.00, 2 rooms cost $500.00, , 3 or more rooms cost $750.00       double floroom1 = 300.00;       double floroom2 = 500.00;       double floroom3 = 750.00; // 3 or more rooms        // delcare pipe burst damage number of pipes: 1 pipe costs $50.00, 2 pipes cost $70.00, , 3 or more pipes cost $100.00       double pipedam1 = 50.00;       double pipedam2 = 70.00;       double pipedam3 = 100.00; // 3 or more pipes        scanner scan = new scanner(system.in);       system.out.print("press 1 if flood or burst pipes occured or press 2 other options: ");       answer = scan.nextint();         switch (answer)       {           case 1:                system.out.print("how many rooms affected flood damage 1, 2, 3 or more?: ");             floanswer = scan.nextint();                system.out.print("how many pipes damaged? 1, 2, 3 or more?: ");             pipeanswer = scan.nextint();               // 1 room damaged             if (floanswer == 1)             {                floodsort = 300.00;                system.out.println("the cost of flood damage " + floroom1 );             }             // 2 rooms damaged             if (floanswer == 2)             {                floodsort = 500.00;                system.out.println("the cost of flood damage " + floroom2 );             }              // 3 or more rooms damaged             if (floanswer >= 3)             {                floodsort = 750.00;                system.out.println("the cost of flood damage " + floroom3 );             }                // 1 pipe damaged             if (pipeanswer == 1)             {                pipesort = 50.00;                system.out.println("the cost of pipe damage " + pipedam1 );             }             // 2 pipes damaged             if (pipeanswer == 2)             {                pipesort = 70.00;                system.out.println("the cost of pipe damage " + pipedam2 );             }              // 3 or more pipes damaged             if (pipeanswer >= 3)             {                pipesort = 100.00;                system.out.println("the cost of pipe damage " + pipedam3 );             }            // determine total cost of visist.             totalcost = floodsort + pipesort;             system.out.println("total cost of visit: $" +totalcost);                break;           case 2:             system.out.println("please contact local office number handles emergency calls, thank you.");//exit program;             break;           default :             system.out.println("invalid choice")   ;             break;       }       }  }// end of class  /*               //  if statement sort flood pricing total                 if (floanswer == 1)                {                floodsort = 300.00;                }                    if (floanswer == 2)                {                floodsort = 500.00;                }                 if (floanswer >= 3)                {                floodsort = 750.00;                }                  //  if statement sort pipe burst pricing total                 if (pipeanswer == 1)                {                pipesort = 50.00;                }                 if (pipeanswer == 2)                {                pipesort = 70.00;                }                 if (pipeanswer >= 3)                {                pipesort = 100.00;                }                  totalcost = (floodsort + pipesort);                system.out.println("total bill comes too: $" + totalcost);                */ 

there no guarantee of if statements hit. since don't assign initial values floodsort , pipesort may never initialized. if want sure initialized can set them 0 @ start

 double floodsort = 0;  double pipesort = 0; 

if rewrite code use arrays instead of separate variables this

import java.util.scanner;  /*author:   date:   program: write class "plumbers" handles emergency plumbing calls. company handles natural floods , burst pipes. if customer selects flood, program must prompt user determine amount of damage pricing. flood charging based on numbers of damaged rooms. 1 room costs $300.00, 2 rooms cost $500.00, , 3 or more rooms cost $750.00. pipe bursting based on number of pipes: 1 pipe costs $50.00, 2 pipes cost $70.00, , 3 or more pipes cost $100.00   */  public class plumbers   {      public static void main(string[] args)     {         //declarations          //double totalcost;         int answer;         int floanswer;         int pipeanswer;         // declare flood room damage cost: 1 room costs $300.00, 2 rooms cost $500.00, , 3 or more rooms cost $750.00         double[] floroom = {300.00,500.00,750};           // delcare pipe burst damage number of pipes: 1 pipe costs $50.00, 2 pipes cost $70.00, , 3 or more pipes cost $100.00         double[] pipedam = {50.00,70.00,100.00};          scanner scan = new scanner(system.in);         system.out.print("press 1 if flood or burst pipes occured or press 2 other options: ");         answer = scan.nextint();          switch (answer)         {             case 1:                  system.out.print("how many rooms affected flood damage 1, 2, 3 or more?: ");                 floanswer = scan.nextint();                 if (floanswer > 3) floanswer = 3;                  system.out.print("how many pipes damaged? 1, 2, 3 or more?: ");                 pipeanswer = scan.nextint();                 if (pipeanswer > 3) pipeanswer = 3;                  double totalcost = floroom[floanswer] + pipedam[pipeanswer];                 system.out.println("total cost of visit: $" +totalcost);                  break;              case 2:                 system.out.println("please contact local office number handles emergency calls, thank you.");//exit program;                 break;              default :                 system.out.println("invalid choice")   ;                 break;         }        }  }// end of class 

Comments

Popular posts from this blog

c# - Update a combobox from a presenter (MVP) -

How to understand 2 main() functions after using uftrace to profile the C++ program? -

How to put a lock and transaction on table using spring 4 or above using jdbcTemplate and annotations like @Transactional? -