用于音符计算的模数和int

时间:2014-03-22 11:51:20

标签: java int modulus

Java输入;

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);


   }


}

屏幕输入和输出;

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

相对较新的编程,

在使用模数和整数运算符让我们在屏幕上使用正确的输出功能方面遇到麻烦,以前的语法正确地工作了50p,任何人都在关心吗?谢谢:))

2 个答案:

答案 0 :(得分:0)

以下是您的代码已更正和改进 不要在这里使用浮点数,使用整数运算。

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);
    }

}

但我会进一步将此简化为(例如)此程序:

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;
        }
    }

}

答案 1 :(得分:0)

尝试将FiftyPence = (int) pay / 2;更改为FiftyPence = (int) (pay / 0.5f);