Java Tip Calculator for newbie -


i hope i'm posting in right place.

i'm pretty new java (meaning third program besides 'hello world').

i have tip calculator i'm working on assignment. i'm not getting 'error' such, method splitting bill seems think each customer pays 'infinity'. have program set in 2 classes: tipcalc1 , tipcalc2 (no points originality of course). program appears run without issue besides 'infinity' issue.

here's have far. assistance appreciated, thanks.

***tipcalc1 class:***  import java.util.scanner;  public class tipcalc1 {       public static void main(string[] args)     {         system.out.println("welcome tip calculator! ");         tipcalc2 calculator = new tipcalc2();         system.out.println("please enter bill amount: ");         tipcalc2.calbill();         system.out.println("what percentage tip?: ");         calculator.perctip();      }  }   ***and tipcalc2 class dirty work:***  import java.util.scanner;  public class tipcalc2 {     static double bill;     double tip;     double total;     double split;     double splitprompt;     double y;     double n;     double billperperson;          static scanner scan = new scanner(system.in);         public static void calbill()         {              bill = scan.nextdouble();         }          public void perctip()         {              tip = scan.nextdouble();             if(tip<1)             {                 total = bill * tip;             }         else total = bill * (tip/100);         system.out.println("your total is: " + total);         split();         }           public void split()         {         system.out.println("would split bill? ");         system.out.println("enter 1 yes or 0 no: ");          splitprompt = scan.nextdouble();         if(splitprompt == 0)         {             system.out.println("your total is: " + total);             system.out.println("thankyou. goodbye.");             system.out.println("end program");           }         if(splitprompt == 1)         {             system.out.println("how many ways split bill? ");             splitprompt = scan.nextdouble();             billperperson = total / split;             system.out.println("each person pays: " + billperperson);             system.out.println("thankyou. goodbye.");             system.out.println("end program.");            }         else system.out.println("invalid entry");         }      } 

the default value split (because have not initialized value) 0.0, therefore, when do

billperperson = total / split; 

you divide 0.0, infinity.

notes:

  • since variable splitprompt double , computers doesn't store real values 100% accuracy, shouldn't compare 0.0. since variable store 0 or 1 input, can declare int, accurate.
  • try follow java naming conventions. use mixedcase methods/variables , use camelcase classes/interfaces.
  • in method split(), should use if-else if-else structure:

    if(splitprompt == 0) { ... } else if(splitprompt == 1) { ... } else { ... } 

Comments

Popular posts from this blog

how to proxy from https to http with lighttpd -

android - Automated my builds -

python - Flask migration error -