Putting code in while or for statement

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • broception
    New Member
    • Feb 2013
    • 4

    Putting code in while or for statement

    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?

    Code:
    import java.util.Scanner;
    
    public class Problem3 {
    
    	//*** User input amount then print out amount in fewest number of bills
    	
    	public static void main(String[] args) {
    		Scanner keyboard=new Scanner(System.in);
    		int amount;
    		int twenty;
    		int ten;
    		int five;
    		int one;
    		
    		System.out.print("Enter amount: ");
    		amount=keyboard.nextInt();
    		
    		twenty = amount/20;
    		amount = amount%20;
    		System.out.println("$20 dollar bills: " + twenty);
    		
    		ten = amount/10;
    		amount = amount%10;
    		System.out.println("$10 dollar bills: " + ten);
    		
    		five = amount/5;
    		amount = amount%5;
    		System.out.println("$5 dollar bills: " + five);
    		
    		one = amount/1;
    		amount = amount%1;
    		System.out.println("$1 dollar bills: " + one);
    		
    
    	}
    
    }
  • r035198x
    MVP
    • Sep 2006
    • 13225

    #2
    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.

    Comment

    Working...