473,397 Members | 2,099 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,397 software developers and data experts.

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?

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
1 1159
r035198x
13,262 8TB
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

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

Similar topics

7
by: Xah Lee | last post by:
# here's a while statement in python. a,b = 0,1 while b < 20: print b a,b = b,a+b --------------- # here's the same code in perl
1
by: jjliu | last post by:
Could someone point me out that why the following perl program only print 'title'. I expect the contents in h1, keywords and description to print out as well. thanks. JJL #!/usr/bin/Perl ...
4
by: muser | last post by:
I have a logical error in my program, I have submitted the program and my tutor hasn't listed the other problems with the code, but said that the program won't run because of a while statement....
4
by: James E Koehler | last post by:
I can't get the WHILE statement to work in MySQL. The version of MySQL that I am using is: Ver 12.16 Distrib 4.0.6-gamma, for Win95/Win98 (i32) running on Windows MX. Here is the relevant...
2
by: herrcho | last post by:
#include <stdio.h> int main() { char c; int length=0; printf("input string : "); gets(c); while(c != '\0'); printf("the length of input string is %d \n", length-1); printf("the number of...
1
by: Tmillwee | last post by:
I have the following while statement in my code While dataReader.read( ‘Do Stuff†End Whil The dataReader contains 19 rows of data, but the while loop continues and fails on a 20th loop. I...
1
by: hdogg | last post by:
Scope Woes - IF statement nested in WHILE statement -PHP I have an array $actuals_sum. <?php while(conditions) { if($i == '24) {
4
by: Villanmac | last post by:
First up thanks to the guys who pointed out my error last week. Now I was wondering If I could get some help with this problem because again I have fallen into trouble. Basically im using the...
2
by: nleake | last post by:
Hey, I've been trying to use Python to help me with some DNA sequencing work. I've figured out how to use Biopython for the importing work; however, I need some python code to help me remove a...
2
by: mar11 | last post by:
hallo all, how could I break the while statement inside the switch statement as follows: enum st {s0, s1} state; while(true) { switch (state) case s0:
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.