By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,653 Members | 1,483 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,653 IT Pros & Developers. It's quick & easy.

Putting code in while or for statement

P: 4
I been working on a program for class, and I finished it but I was wondering if I could put the code into a while statement to make it easier to read. If it's possible, can someone show me how?

Expand|Select|Wrap|Line Numbers
  1. import java.util.Scanner;
  2.  
  3. public class Problem3 {
  4.  
  5.     //*** User input amount then print out amount in fewest number of bills
  6.  
  7.     public static void main(String[] args) {
  8.         Scanner keyboard=new Scanner(System.in);
  9.         int amount;
  10.         int twenty;
  11.         int ten;
  12.         int five;
  13.         int one;
  14.  
  15.         System.out.print("Enter amount: ");
  16.         amount=keyboard.nextInt();
  17.  
  18.         twenty = amount/20;
  19.         amount = amount%20;
  20.         System.out.println("$20 dollar bills: " + twenty);
  21.  
  22.         ten = amount/10;
  23.         amount = amount%10;
  24.         System.out.println("$10 dollar bills: " + ten);
  25.  
  26.         five = amount/5;
  27.         amount = amount%5;
  28.         System.out.println("$5 dollar bills: " + five);
  29.  
  30.         one = amount/1;
  31.         amount = amount%1;
  32.         System.out.println("$1 dollar bills: " + one);
  33.  
  34.  
  35.     }
  36.  
  37. }
  38.  
Feb 26 '13 #1
Share this Question
Share on Google+
1 Reply


10K+
P: 13,264
You only use while or for when there is a loop. You don't have a loop in the code. You can improve the readability however by declaring variables as close to where they are used as possible.
Mar 6 '13 #2

Post your reply

Sign in to post your reply or Sign up for a free account.