473,387 Members | 1,394 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,387 software developers and data experts.

Loop Repeat?

I'm writing a program where it asks a user a number between a range and then displays $ the number of times of the number entered.

But I'm needing to have this repeat 5 times (or more if an invalid number is entered).


EDIT:::: Got the repeat fixed, just needing to have it repeat if it goes to the else statement.


Here is my code so far.

Expand|Select|Wrap|Line Numbers
  1. import java.util.Scanner;
  2. public class Lab7_Ex1
  3. {
  4.     public static void  main(String[] args)
  5.     {
  6.  
  7.         Scanner keyboard = new Scanner(System.in);
  8.         int x;
  9.         int count=1;
  10.  
  11.         for (count = 5; count<=5; count++)
  12.         {
  13.             System.out.print("Enter an integer in the range 1-10: ");
  14.             x = keyboard.nextInt();
  15.  
  16.             if ( x >= 1 && x <= 10)
  17.                 {     
  18.                     for (int i = 1; x<=1; i++);
  19.                     {
  20.                         for (int j = 1; j <= x; j++)
  21.                         {
  22.                           System.out.print('$');
  23.                   }
  24.                   System.out.println();
  25.                }
  26.                 }
  27.             else
  28.                 {
  29.                     System.out.println("Invalid number");
  30.                 }
  31.             }
  32.         }
  33.     }        
  34.  
  35.  
Mar 30 '09 #1
3 7900
JosAH
11,448 Expert 8TB
I didn't read your code but my eye was attracted to this line:

Expand|Select|Wrap|Line Numbers
  1.  
  2. for (count = 5; count<=5; count++) 
  3.  
You probably intended to write:

Expand|Select|Wrap|Line Numbers
  1.  
  2. for (count= 0; count < 5; count++) 
  3.  
kind regards,

Jos
Mar 30 '09 #2
Thanks that helped but how do I make it repeat if the number is invalid?
Mar 30 '09 #3
JosAH
11,448 Expert 8TB
@Kamaria
Don't increment the loop counter if the number is not valid; something like this:

Expand|Select|Wrap|Line Numbers
  1. for (int count= 0; count < 5; ) { // <-- see? no increment
  2.    // obtain a number
  3.    if (isValid()) {
  4.       // do some work
  5.       count++; // increment counter
  6.    }
  7.    else {
  8.       // do some other work and don't increment
  9.    }
  10. }
  11.  
kind regards,

Jos
Mar 31 '09 #4

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

Similar topics

9
by: Itay | last post by:
Hi all , Suppose i have structurs defines as follows : struct S1 { int n1 ; int n2 ; int n3 ; };
43
by: Gremlin | last post by:
If you are not familiar with the halting problem, I will not go into it in detail but it states that it is impossible to write a program that can tell if a loop is infinite or not. This is a...
14
by: Crimsonwingz | last post by:
Need to calculate a sum based on a number of factors over a period of years. I can use formula ^x for some of it, but need totals to carry over in the sum and have only been able to do this thus...
8
by: Microsoft | last post by:
I have the following loop the length contains somewhere around 1000+ items: For elemIndex = 0 To len - 1 elem = CType(doc.all.item(elemIndex), mshtml.IHTMLElement) If elem.tagName = "A" Then If...
17
by: David Isaac | last post by:
I would like to be able to define a loop statement (nevermind why) so that I can write something like loop 10: do_something instead of for i in range(10): do_something
5
by: é›· | last post by:
suggest add do while loop in later version
35
by: erik gartz | last post by:
Hi. I'd like to be able to write a loop such as: for i in range(10): pass but without the i variable. The reason for this is I'm using pylint and it complains about the unused variable i. I can...
23
by: Sacred Heart | last post by:
Hi, I'm new to Python and have come across a problem I don't know how to solve, enter com.lang.python :) I'm writing some small apps to learn the language, and I like it a lot so far. My...
3
by: numlock00 | last post by:
I have a nested 'while' loop that won't repeat, no matter how many times the outer loop repeats. The outer loop reads through an array of elements; the inner loop Ithe 'while' loop) is supposed to...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
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...

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.