转动便士改变

时间:2014-11-30 23:26:03

标签: java

我目前正在开始学习Java并且正在制作能够改变便士的代码。我已经完成了它但是我遇到的一个小问题是,当我输入1p或超过501p时,它将显示正确的消息但仍然在消息下方执行计算。

如果有任何其他问题可以指出,我也很感激:)

我的代码是:

class Main { 
public static void main( String args[] ) {

    System.out.print("#Please enter the amount of change : ");
    int change = BIO.getInt();

    while(change > 0)
    {
        int twopounds, pounds, fifty, twenty, ten, five, two, one;

        twopounds = change / 200;
        int left = change % 200;

        pounds = left / 100;
        left = left % 100;

        fifty = left / 50;
        left = left % 50;

        twenty = left / 20;
        left = left % 20;

        ten = left / 10;
        left = left % 10;

        five = left / 5;
        left = left % 5;

        two = left / 2;
        left = left % 2;

        one = left / 1;

        int nbCoins = twopounds + pounds + fifty + twenty + ten + five + two + one;

        if (change == 1)
        {
            System.out.print("1 coin" + "\n");
        }

        if (change > 500)
        { 
            System.out.print("Invalid amount " + change + "p" + "\n");
        }

        if (change <= 500 && change > 1)

            System.out.print(change + "p " + nbCoins +" coins ");

        {

            if ( twopounds > 0 )
            {
                System.out.print( twopounds > 1 ? twopounds + "*200p " : "200p " );
            }

            if ( pounds > 0 )
            {
                System.out.print( pounds > 1 ? pounds + "*100p " : "100p " );
            }

            if ( fifty > 0 )
            {
                System.out.print( fifty > 1 ? fifty + "*50p " : "50p " );
            }

            if ( twenty > 0 )
            {
                System.out.print( twenty > 1 ? twenty + "*20p " : "20p " );
            }

            if ( ten > 0 )
            {
                System.out.print( ten > 1 ? ten + "*10p " : "10p " );
            }

            if ( five > 0 )
            {
                System.out.print( five > 1 ? five + "*5p " : "5p " );
            }

            if ( two > 0 )
            {
                System.out.print( two > 1 ? two + "*2p " : "2p " );
            }

            if ( one > 0 )
            {
                System.out.print( one > 1 ? one + "*1p " : "1p " );
            }

            System.out.print("\n");
        }
        System.out.print("#Please enter the amount of change : ");
        change = BIO.getInt();
    }

}
}

谢谢!

3 个答案:

答案 0 :(得分:0)

问题在于您的“if”声明:

if (change <= 500 && change > 1)

        System.out.print(change + "p " + nbCoins +" coins ");

仅包含第一个“打印”。看起来您打算将其余的if语句保留在其中,因为它包围了{}

如果将System.out.print(change + "p " + nbCoins +" coins ");移到{之后的行(基本上是两行),那么整个过程应该按预期工作。

答案 1 :(得分:0)

如果if语句中只有一个语句,则if语句不需要花括号。那么你的最后一个if语句在哪里:

if (change <= 500 && change > 1)

    System.out.print(change + "p " + nbCoins +" coins ");

{
    //your other if statements
}

if基本上改为:

if (change <= 500 && change > 1)
{
    System.out.print(change + "p " + nbCoins +" coins ");
}

和大括号中的其余ifs在主函数的范围之外执行:

{
    if ( twopounds > 0 )
    {
        System.out.print( twopounds > 1 ? twopounds + "*200p " : "200p " );
    }

    if ( pounds > 0 )
    {
        System.out.print( pounds > 1 ? pounds + "*100p " : "100p " );
    }

    //Rest of your ifs
}

超出范围我的意思是主要不能访问在函数中创建的变量。如果你在那些花括号中创建了一个新变量,那么主要不能看到它们。

答案 2 :(得分:0)

你的循环似乎是不必要的,因为它是目前的。您可以通过使用

的循环来删除大量(重复)代码
int[] change = {200, 100, ... , 2, 1};
int changeDue = BIO.getInt();
String changeGiven = "";

for (int i = 0; i < change.length(); arr++)
{
    int n = arr[i];
    int k = changeDue / n;

    changeGiven += String.format(
        "%s%s%i%s\n",
        (n / 100 >= 1 ? "£" : ""), 
        (k > 1 ? k + "*" : ""),
        (n / 100 >= 1 ? n / 100 : n),
        (n / 100 < 1 ? "p" : "")
    );          
}

System.out.println(changeGiven);