Q BgQuestion:

      
Novice
Karma Points: 25
Respect (100%):
posted by  jrupe08 on 11/5/2009 1:05:13 AM  |  status: Closed  |  Earned Karma: 25

Simple Java Problem involving loops

Course Textbook Chapter Problem Needs by
N/A N/A N/A N/A 11/5/2009 at 10:00:00 AM
Question Details:
Complete the following code segment using only one loop so that it will print the following pattern (shown for n = 4):

****
***
**
*

There are no spaces on the first line of output. You may assume that the user types a number between 1 and 50. Use the variable names provided.

Scanner scan = new Scanner(System.in);
System.out.println(“Enter the number of lines”);
int n = scan.nextInt( ), i;
String spaces = “”;
String stars = “**************************************************”;
Bonus Point Alert! Earn +7 additional karma points for helping this gold member.

AAnswers:

Answer Question Ask for clarification
Mentor
Karma Points: 607
posted by St. John on 11/5/2009 1:52:15 AM  |  status: Live
Asker's Rating: Lifesaver   
Response Details:
I am not sure what you are meant to do with the spaces variable.
You never said anything about it.
Here is everything else though.

stjohn@beatific ~/programming/java $ java Stars
Enter the number of lines
4
****
***
**
*
stjohn@beatific ~/programming/java $ cat Stars.java
import java.util.Scanner;

public class Stars
{
        public static void main(String[] args)
        {
                Scanner scan = new Scanner(System.in);
                System.out.println("Enter the number of lines");
                int n = scan.nextInt(), i;
                String spaces = "";
                String stars = "**************************************************";

                for (i=n; i>0; i--)
                {
                        System.out.println(stars.substring(0,i));
                }
        }
}
stjohn@beatific ~/programming/java $
Answer Question Ask for clarificarion

Join Cramster's Community

Cramster.com brings together students, educators and subject enthusiasts in an online study community. With around-the-clock expert help and a community of over 100,000 knowledgeable members, you can find the help you need, whenever you need it. Join for free today » How Cramster is different from tutoring »