Hey all. I hope I'm posting in the right place.
I'm pretty new to Java (meaning this is only my third program besides 'hello world').
I have a tip calculator I'm working on for an assignment. I'm not getting an 'error' as such - the program runs, but the result is consistently wrong.
I'm wondering if it's to do with the 'split' variable being a double, since the 'total' amount seems to be correct when run. The issue seems to be when it tries to divide the 'total' by 'split' - and consistently returns 'infinity' in the console.
I have my program set up in two classes: tipCalc1 and tipCalc2 (no points for creative names of course). Here's what I have so far. Any assistance appreciated, thanks.
TipCalc1
import java.util.Scanner; public class Tipcalc1 { public static void main(String[] args) { System.out.println("Welcome to Tip Calculator! "); TipCalc2 Calculator = new TipCalc2(); System.out.println("Please enter the bill amount: "); TipCalc2.calBill(); System.out.println("What percentage would you like to tip?: "); Calculator.percTip(); } }
And the tipCalc2 class which does the dirty work:
import java.util.Scanner; public class TipCalc2 { static double bill; double tip; double total; //should 'split' be a double?// 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 you like to split the bill? "); System.out.println("Enter 1 for YES or 0 for 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 would you like to split the bill? "); splitPrompt = scan.nextDouble(); //Problem may lie here, or further up where the split variable is defined (as double).// 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"); } }