The Artima Developer Community
Sponsored Link

Java Answers Forum
HELP: Counting Down Using a Loop

1 reply on 1 page. Most recent reply: Sep 26, 2002 1:07 AM by siddhartha

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 1 reply on 1 page
Brook

Posts: 12
Nickname: brook
Registered: Sep, 2002

HELP: Counting Down Using a Loop Posted: Sep 25, 2002 4:24 PM
Reply to this message Reply
Advertisement
Experience .... Java Beginner: I want to write a few lines where I can count down using a loop. Example: The System.out.println would be like "I have 100 dimes". Using the console (I believe) I would have a User type in a number between 1 and 100. We'll say the User types in 20 and presses Enter. The printout that the User would see would be this:
I have 100 dimes
I have 99 dimes
I have 98 dimes
I have 97 dimes .... and so on down to "I have 80 dimes".

I'm really lost as to even know where to start. Can anyone help? THANKS.


siddhartha

Posts: 15
Nickname: saddysan
Registered: Jul, 2002

Re: HELP: Counting Down Using a Loop Posted: Sep 26, 2002 1:07 AM
Reply to this message Reply
Here try this, hope this might help you

import java.io.*;

class LoopTest
{
public static void main(String[] args)
{
try
{
System.out.println("Enter a number:");
BufferedReader br = new BufferedReader(new
InputStreamReader(System.in));
String str;
str = br.readLine();
int num = new Integer(str).intValue();
if(num<0)
{
System.out.println("Error: Enter Positive Values Only");
return;
}
num = 100-num;
for(int i=100;i>=num;i--)
System.out.println("I have "+i+" dimes");
}
catch(IOException io)
{
io.printStackTrace();
}
catch(NumberFormatException e)
{
System.out.println("Enter Integer only");
}
}
}

Flat View: This topic has 1 reply on 1 page
Topic: URGENT!!! one day left. Previous Topic   Next Topic Topic: Plug in for Java Applets

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use