Results 1 to 4 of 4
  1. #1
    GreenJava is offline Member
    Join Date
    Jun 2014
    Rep Power

    Default Java loop design I guess?

    Basically I have to enter 5 numbers that I put through a loop and they print the star * depending on the number.
    An example would be this 5:*****. However, my codes prints out 5:*; 5 times. How to correct my code

    Java Code:
    import java.util.Scanner; 
    public class IntegerOutput
    public static void main( String args[] ) 
    Scanner input = new Scanner( ); 
    int num1; 
    int num2; 
    int num3; 
    int num4; 
    int num5; 
    System.out.print( "Enter five positive integers, with a single space in between each."); 
    num1 = input.nextInt(); 
    num2 = input.nextInt(); 
    num3 = input.nextInt(); 
    num4 = input.nextInt(); 
    num5 = input.nextInt(); 
    for(int x = 1; x <= num1; x++)
    	System.out.println(num1 +":*");
    for(int x = 1; x <= num2; x++)
    	System.out.println(num2 +":*");
    for(int x = 1; x <= num3; x++)
    	System.out.println(num3 +":*");
    for(int x = 1; x <= num4; x++)
    	System.out.println(num4 +":*");
    for(int x = 1; x <= num5; x++)
    	System.out.println(num5 +":*");

  2. #2
    kneitzel is offline Senior Member
    Join Date
    Feb 2014
    Rep Power

    Default Re: Java loop design I guess?


    just look at your code:
    you have a for loop and inside the loop you print the num1 and the star.
    If you only want to print the num1: one time, you have to move that outside the loop so that you only print the star inside the loop.


  3. #3
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Rep Power

    Default Re: Java loop design I guess?

    Java Code:
    for(int x = 1; x <= num1; x++)
        System.out.println(num1 +":*");
    Is it really such a big mystery to you why your code is doing that? This code reads in plain English. Instead of focusing on code, bring it back to English.

    - I want to print the number I input once like 'X:'.
    - after that I want to print X stars, where X is the number input

    From the description alone you'll see that you need at least two print statements to get one result line.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  4. #4
    jim829 is online now Senior Member
    Join Date
    Jan 2013
    Northern Virginia, United States
    Rep Power

    Default Re: Java loop design I guess?

    And I recommend you review the different types of print methods available in System.out. As you read them a light bulb should go on as to why your program is not printing like you described.

    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

Similar Threads

  1. Game Loop design
    By rru96 in forum Java Gaming
    Replies: 4
    Last Post: 01-05-2014, 10:17 PM
  2. guess # program
    By shin777 in forum New To Java
    Replies: 5
    Last Post: 10-30-2013, 09:55 PM
  3. help needed plz with some loop problem I guess
    By Tommeke in forum New To Java
    Replies: 6
    Last Post: 07-21-2011, 07:42 PM
  4. Guess Character
    By pinkdreammsss in forum Java Applets
    Replies: 14
    Last Post: 05-05-2010, 07:28 PM
  5. Help about Guess the Numbers Program in java
    By macfrik in forum New To Java
    Replies: 6
    Last Post: 03-25-2009, 04:59 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts