简体   繁体   English

为什么该程序在50%的时间中仅输出1便士和2便士?

[英]Why does this program only output 1 pence and 2 pence 50% of the time?

public class Bankteller2 {
public static void main(String args[]){
    //declares and initialise variables
    double Sterling = 188.23;
    String indent = "";
    double Hundreds, Fifties, Twenties, Tens, Fives, Ones, fifty_pence, twenty_pence, ten_pence, five_pence, two_pence, one_pence;

    //converts sterling amount into a working number of pounds and pence
    Ones =  Sterling;


    Hundreds =  Ones / 100;
    Ones =  Ones % 100;
    one_pence = Ones % 1;

    Fifties =  Ones / 50;
    Ones =  Ones % 50;
    one_pence = Ones % 1;


    Twenties =  Ones/20;
    Ones =  Ones % 20;
    one_pence = Ones % 1;


    Tens =  Ones / 10;
    Ones =  Ones % 10;
    one_pence = Ones % 1;


    Fives =  Ones / 5;
    Ones =  Ones % 5; 
    one_pence = (Ones % 1) *100;


    fifty_pence = one_pence / 50;
    one_pence =  one_pence % 50;


    twenty_pence = one_pence / 20;
    one_pence =  one_pence % 20;


    ten_pence =  one_pence / 10;
    one_pence =  one_pence % 10;


    five_pence =  one_pence / 5;
    one_pence =  one_pence % 5;


    two_pence =  one_pence / 2;
    one_pence =  one_pence % 2;


    //output results
    System.out.println("The notes are :");
    System.out.println();
    System.out.println(indent + "Hundreds :"+(Math.floor( Hundreds)));
    System.out.println(indent + "Fifties :"+(Math.floor( Fifties)));
    System.out.println(indent + "Twenties :"+(Math.floor( Twenties)));
    System.out.println(indent +"Tens :" +(Math.floor( Tens)));
    System.out.println(indent + "Fives :"+(Math.floor( Fives)));
    System.out.println(indent +"One pound coins :"+(Math.floor( Ones)));
    System.out.println(indent +"Fifty pence :"+(Math.floor( fifty_pence)));
    System.out.println(indent +"Twenty pence :"+(Math.floor( twenty_pence)));
    System.out.println(indent +"Ten pence :"+(Math.floor( ten_pence)));
    System.out.println(indent +"Five pence :"+(Math.floor( five_pence)));
    System.out.println(indent +"Two pence :"+(Math.floor( two_pence)));
    System.out.println(indent +"One pence :"+(Math.floor( one_pence)));
    System.out.println();
    System.out.println("Total: "+ Sterling + " ");
} }

the output i get is as following: 我得到的输出如下:

The notes are :

Hundreds :1.0
Fifties :1.0
Twenties :1.0
Tens :1.0
Fives :1.0
One pound coins :3.0
Fifty pence :0.0
Twenty pence :1.0
Ten pence :0.0
Five pence :0.0
Two pence :1.0
One pence :0.0

Total: 188.23 

however there should be 1 in the One pence output. 但是,一便士输出中应该有1。 Similar things happen when other "pence" values are inputted below 10 and above 50. 当输入其他“便士”值低于10且高于50时,也会发生类似的情况。

As Marko Topolnik said in his comment, don't use doubles for money calculations. 正如Marko Topolnik在他的评论中所说,不要在金钱计算中使用双精度。

Here's your code modified to use integers for the calculations. 这是将您的代码修改为使用整数进行计算的代码。 Java variables start with a lower case letter. Java变量以小写字母开头。 That's so you and us can tell the difference between a class name and a variable name. 这样一来,您和我们就可以分辨出类名和变量名之间的区别。

package com.ggl.testing;

public class Bankteller2 {
    public static void main(String args[]) {
        // declares and initialise variables
        double sterling = 188.23;
        String indent = "";
        int hundreds, fifties, twenties, tens, fives, ones;
        int fifty_pence, twenty_pence, ten_pence, five_pence, two_pence, one_pence;

        // converts sterling amount into a working number of pounds and pence
        ones = (int) Math.round(sterling * 100D);

        hundreds = ones / 10000;
        ones = ones % 10000;

        fifties = ones / 5000;
        ones = ones % 5000;

        twenties = ones / 2000;
        ones = ones % 2000;

        tens = ones / 1000;
        ones = ones % 1000;

        fives = ones / 500;
        ones = ones % 500;

        one_pence = ones % 100;
        ones = ones / 100;

        fifty_pence = one_pence / 50;
        one_pence = one_pence % 50;

        twenty_pence = one_pence / 20;
        one_pence = one_pence % 20;

        ten_pence = one_pence / 10;
        one_pence = one_pence % 10;

        five_pence = one_pence / 5;
        one_pence = one_pence % 5;

        two_pence = one_pence / 2;
        one_pence = one_pence % 2;

        // output results
        System.out.println("The notes are :");
        System.out.println();
        System.out.println(indent + "Hundreds : " + hundreds);
        System.out.println(indent + "Fifties : " + fifties);
        System.out.println(indent + "Twenties : " + twenties);
        System.out.println(indent + "Tens : " + tens);
        System.out.println(indent + "Fives : " + fives);
        System.out.println(indent + "One pound coins : " + ones);
        System.out.println(indent + "Fifty pence : " + fifty_pence);
        System.out.println(indent + "Twenty pence : " + twenty_pence);
        System.out.println(indent + "Ten pence : " + ten_pence);
        System.out.println(indent + "Five pence : " + five_pence);
        System.out.println(indent + "Two pence : " + two_pence);
        System.out.println(indent + "One pence : " + one_pence);
        System.out.println();
        System.out.println("Total: " + sterling);
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM