为什么这个程序在50%的时间内只输出1便士和2便士?

时间:2015-01-27 14:54:19

标签: java eclipse

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

但是在便士输出中应该有1。 当其他“便士”值输入低于10且高于50时,也会发生类似的事情。

1 个答案:

答案 0 :(得分:0)

正如Marko Topolnik在评论中所说,不要使用双打进行金钱计算。

这是您的代码修改为使用整数进行计算。 Java变量以小写字母开头。这就是你和我们可以区分一个类名和一个变量名。

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