Shortening My While Loops

Hi there, recently I wrote a programme that worked fine but it seemed overly long, im pretty new to java and all and I was wondering how I could shorten this programmes while loops so it does the same thing only the code is shorter

class ChangeMaker
{
public static void main(String[] args)
{
System.out.println("How much change you got?");
int money = Console.readInt();
int fifty = 0;
int twenty = 0;
int ten = 0;
int five = 0;
int two = 0;
int one = 0;

while (money < 100 && money > 49)
{
money = money - 50;
fifty = fifty + 1;
}

while (money < 50 && money > 19)
{
money = money - 20;
twenty = twenty + 1;
}

while (money < 20 && money > 9)
{
money = money - 10;
ten = ten + 1;
}

while (money < 10 && money > 4)
{
money = money - 5;
five = five + 1;
}

while (money < 5 && money > 1)
{
money = money - 2;
two = two + 1;
}

while (money < 2 && money > 0)
{
money = money - 1;
one = one + 1;
}
System.out.println("50's "+fifty);
System.out.println("20's "+twenty);
System.out.println("10's "+ten);
System.out.println("5's "+five);
System.out.println("2's "+two);
System.out.println("1's "+one);
System.out.println(money);
}
}

Comments

  • : Hi there, recently I wrote a programme that worked fine but it seemed overly long, im pretty new to java and all and I was wondering how I could shorten this programmes while loops so it does the same thing only the code is shorter
    :
    : class ChangeMaker
    : {
    : public static void main(String[] args)
    : {
    : System.out.println("How much change you got?");
    : int money = Console.readInt();
    : int fifty = 0;
    : int twenty = 0;
    : int ten = 0;
    : int five = 0;
    : int two = 0;
    : int one = 0;
    :
    : while (money < 100 && money > 49)
    : {
    : money = money - 50;
    : fifty = fifty + 1;
    : }
    :
    : while (money < 50 && money > 19)
    : {
    : money = money - 20;
    : twenty = twenty + 1;
    : }
    :
    : while (money < 20 && money > 9)
    : {
    : money = money - 10;
    : ten = ten + 1;
    : }
    :
    : while (money < 10 && money > 4)
    : {
    : money = money - 5;
    : five = five + 1;
    : }
    :
    : while (money < 5 && money > 1)
    : {
    : money = money - 2;
    : two = two + 1;
    : }
    :
    : while (money < 2 && money > 0)
    : {
    : money = money - 1;
    : one = one + 1;
    : }
    : System.out.println("50's "+fifty);
    : System.out.println("20's "+twenty);
    : System.out.println("10's "+ten);
    : System.out.println("5's "+five);
    : System.out.println("2's "+two);
    : System.out.println("1's "+one);
    : System.out.println(money);
    : }
    : }
    :
    :
    You should use the integer division operators instead of while-loops. That way you can calculate each of the number of change in 2 steps. I don't know any java, but in javascript it would be something like this:
    [code]
    var money = Console.readInt();
    var fifty = Math.floor(money/50);
    money = money%50;
    [/code]
    Remember Java and Javascript are two quite different languages. Next time it might be helpful to ask the question on the java board.
Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

In this Discussion