-1

Java input;

import java.util.*;

public class NetPay3
  {
    public static void main()
    {
      // Define Scanner object
      Scanner inLine = new Scanner (System.in);

      // Define other variables
      float pay; 
      int OneHundredPounds, FiftyPounds, TwentyPounds, FivePounds,
      OnePound, FiftyPence, TwentyPence, FivePence, TwoPence, OnePenny;

      // Ask for the time in seconds
      System.out.print ("Enter Net Pay : ");
      pay = inLine.nextFloat();


      // Calculate the hours.  There are (3600) 
      // i.e. 60 x 60 seconds in every hour
      OneHundredPounds = (int) pay / 100;

      // Calculate what is left over and store back into seconds
      pay = pay % 100;

      // Calculate the minutes.  There are 60 seconds
      // in a minute.
      FiftyPounds = (int) pay / 50;

      // Whatever is left over must be the seconds
      pay = pay % 50;

      // Calculate the hours.  There are (3600) 
      // i.e. 60 x 60 seconds in every hour
      TwentyPounds = (int) pay / 20;

      // Calculate what is left over and store back into seconds
      pay = pay % 20;

      // Calculate the hours.  There are (3600) 
      // i.e. 60 x 60 seconds in every hour
      FivePounds = (int) pay / 5;

      // Calculate what is left over and store back into seconds
      pay = pay % 5;

      // Calculate the hours.  There are (3600) 
      // i.e. 60 x 60 seconds in every hour
      OnePound = (int) pay / 1;

      // Calculate what is left over and store back into seconds
      pay = pay % 1;

      // Calculate the hours.  There are (3600) 
      // i.e. 60 x 60 seconds in every hour
      FiftyPence = (int) pay / 2;

      // Calculate what is left over and store back into seconds
      pay = pay % 2;

      // Display the hours, minutes and seconds
      System.out.println ("Amount of £100 notes " + OneHundredPounds);
      System.out.println ("Amount of £50 notes " + FiftyPounds);
      System.out.println ("Amount of £20 notes " + TwentyPounds);
      System.out.println ("Amount of £5 notes " + FivePounds);
      System.out.println ("Amount of £1 coins " + OnePound);
      System.out.println ("Amount of 50p coins " + FiftyPence);


   }


}

Screen input and output;

Enter Net Pay : 176.50
Amount of £100 notes 1
Amount of £50 notes 1
Amount of £20 notes 1
Amount of £5 notes 1
Amount of £1 coins 1
Amount of 50p coins 0

Hi relatively new to programming,

having trouble with me modulus and int operators in terms of getting them to function with the correct output on screen, previous syntax's worked correctly bar the 50p, anyone care to shed any light? thanks :)

Florent Bayle
  • 11,520
  • 4
  • 34
  • 47

2 Answers2

0

Here is your code corrected and improved.
Don't use floats here, use integer arithmetic.

import java.util.*;

public class NetPay3 {
    public static void main(String[] args) {
        // Define Scanner object
        Scanner inLine = new Scanner(System.in);

        // Define other variables
        int pay;
        int OneHundredPounds, FiftyPounds, TwentyPounds, FivePounds, OnePound, FiftyPence, TwentyPence, FivePence, TwoPence, OnePenny;

        System.out.print("Enter Net Pay : ");
        float pay1 = inLine.nextFloat();

        pay = (int) (100 * pay1);

        OneHundredPounds = (int) pay / 10000;
        pay = pay % 10000;

        FiftyPounds = (int) pay / 5000;
        pay = pay % 5000;

        TwentyPounds = (int) pay / 2000;
        pay = pay % 2000;

        FivePounds = (int) pay / 500;
        pay = pay % 500;

        OnePound = (int) pay / 100;
        pay = pay % 100;

        FiftyPence = (int) pay / 50;
        pay = pay % 50;

        System.out.println("Amount of £100 notes " + OneHundredPounds);
        System.out.println("Amount of £50 notes " + FiftyPounds);
        System.out.println("Amount of £20 notes " + TwentyPounds);
        System.out.println("Amount of £5 notes " + FivePounds);
        System.out.println("Amount of £1 coins " + OnePound);
        System.out.println("Amount of 50p coins " + FiftyPence);
        System.out.println("Leftover pence: " + pay);
    }

}

But I would further simplify this to (for example) this program:

import java.util.*;

public class NetPay3 {
    public static void main(String[] args) {
        Scanner inLine = new Scanner(System.in);
        float[] val = new float[]{100, 50, 20, 5, 1, 0.5f, 0.2f, 0.05f, 0.02f, 0.01f};

        int pay;

        System.out.print("Enter Net Pay : ");
        float pay1 = inLine.nextFloat();

        pay = (int) (100 * pay1);

        for (int i=0; i<val.length; i++){
            int m = ((int)(val[i] * 100));
            int cnt = pay / m;
            String s1 = val[i] < 1 ? " coins: " : " notes: ";
            String s2 = val[i] < 1 ? "" : "£";
            String s3 = val[i] < 1 ? "p" : "";
            String s4 = val[i] < 1 ? m + "" : (m/100) + "";

            System.out.println("Amount of " + s2 + s4 + s3 + s1 + cnt);

            pay = pay % m;
        }
    }

}
peter.petrov
  • 38,363
  • 16
  • 94
  • 159
0

Try changing FiftyPence = (int) pay / 2; toFiftyPence = (int) (pay / 0.5f);

wns349
  • 1,266
  • 1
  • 10
  • 20
  • I suppose peter.petrov's answer is better. My answer will just solve your problem without editing too much of existing code. – wns349 Mar 22 '14 at 12:15