Search code examples
javamath

How would I get my pennies to double each loop?


    public static void main(String[] args) {
    
        
        //Variables
        int worked;
        double pennies,pay,totalpay;
            
        pay=0;
        totalpay=0.0;   
        pennies=0.01;
        //Scanner object for input.
        Scanner keyboard = new Scanner(System.in);  
        

        //Get days worked.
        System.out.println("How many days have you worked?");
        worked=keyboard.nextInt();
        
        //Display error and instruct to fix.
        while (worked < 1) 
        {
        System.out.println("Invalid input. Please enter a number no less than one.");
        worked=keyboard.nextInt();
        }

        //Display the headings
        System.out.println("Days\t\tPennies");
        System.out.println("------------------------");
                
        //Display for Pennies * Days        
        for (int days=1; days <= worked; days++)
        {
            //The Math.
            totalpay+=pennies;
            pennies*=2;
            //Display
            System.out.printf(days+"\t\t$%,.2f\n",totalpay);
            
            
            if (days>=worked)
            System.out.printf("\n\nYour Total Pay is:\t$%,.2f",totalpay);
            
        }
        
    }

}

Class and package hidden for personal information reasons.

I've tried various different formulas to do the math but my math isn't mathing.

I need the penny value to double daily. first day you'd make .01 second day you would make .02 third day you would make .04 fourth day .08 etcetcetc.

Every time I change the math in the program I just keep getting one crazy number after another.

(Java)


Solution

  • Seems to be very trivial but still I will go ahead and answer, looks like you dont even need to calculate totalPay, your pennies variable will hold the latest data for you.

    public static void main(String[] args) {
    
        
        //Variables
        int worked;
        double pennies,pay,totalpay;
            
        pay=0;
        totalpay=0.0;   
        pennies=0.01;
        //Scanner object for input.
        //Scanner keyboard = new Scanner(System.in);  
        
    
        //Get days worked.
        System.out.println("How many days have you worked?");
        worked=20;
        
       
        //Display the headings
        System.out.println("Days\t\tPennies");
        System.out.println("------------------------");
                
        //Display for Pennies * Days        
        for (int days=1; days <= worked; days++)
        {
            //Display
            System.out.printf(days+"\t\t$"+pennies+"\n");
            pennies*=2;
            
            
            if (days>=worked)
            System.out.printf("\n\nYour Total Pay is:\t$%,.2f",totalpay);
            
        }
        
    }
    

    This will work well till some 30 days, after that scientific notations will start getting printed, you might want to use a BigDecimal for supporting larger number of days.

    Hope this helps!

    Good luck!