4 Replies - 688 Views - Last Post: 12 August 2014 - 09:35 PM Rate Topic: -----

#1 hadamic   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 15
  • Joined: 11-August 14

Ask user for number of rows/give user option to repeat the program

Posted 11 August 2014 - 05:10 AM

This is what I have so far, but how do I ask the user for the number or rows and
give the user the option to repeat the program?

public class Pyramid {

public static void main(String[] args) {
int myLevel;
int i, j , k;

myLevel = 6;

for (i = 1; i <= myLevel; i++) {

for (k =1; k <= myLevel-i; k++)

System.out.print(" ");

for (j = k+1; j <=myLevel; j++) 

System.out.print( "*");
for(int l=myLevel;l>k-1;l--)
System.out.print( "*");

System.out.println("");

}


}
}

This post has been edited by JackOfAllTrades: 11 August 2014 - 05:21 AM
Reason for edit:: Fixed code tags


Is This A Good Question/Topic? 0
  • +

Replies To: Ask user for number of rows/give user option to repeat the program

#2 Barrett122   User is offline

  • D.I.C Head

Reputation: 24
  • View blog
  • Posts: 98
  • Joined: 22-February 13

Re: Ask user for number of rows/give user option to repeat the program

Posted 11 August 2014 - 05:21 AM

To take input from the user you can use the Scanner class.
A quick example might be:
Scanner scanner = new Scanner(System.in);
int userInput = scanner.nextInt();
scanner.close();


This will read the next integer the user types and store it in the variable userInput.
Was This Post Helpful? 0
  • +
  • -

#3 hadamic   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 15
  • Joined: 11-August 14

Re: Ask user for number of rows/give user option to repeat the program

Posted 12 August 2014 - 09:15 PM

I'm still having problems repeating the program. This is what I have so far, but I keep running into errors:

import java.util.Scanner;

public class Pyramid 
{
   
//Displaying a Pyramid
    public static void main(String[] args) 
    {
      Scanner scanner = new Scanner(System.in);
      //Get user input = number of rows to print in a pyramid
      System.out.print("Enter an integer for the number of rows: ");
      int userInput = scanner.nextInt();
      scanner.close();

      int myLevel;
      int i, j , k;

      myLevel = userInput;

      for (i = 1; i <= myLevel; i++) {

      for (k =1; k <= myLevel-i; k++)

      System.out.print(" ");

      for (j = k+1; j <=myLevel; j++) 

      System.out.print( "*");
      for(int l=myLevel;l>k-1;l--)
      System.out.print( "*");

      System.out.println("");
      System.out.print("Do you want to continue? (y/n): ");
      choice = scanner.next().charAt(0);
      }
   }
}


Was This Post Helpful? 0
  • +
  • -

#4 macosxnerd101   User is offline

  • Games, Graphs, and Auctions
  • member icon




Reputation: 12305
  • View blog
  • Posts: 45,401
  • Joined: 27-December 08

Re: Ask user for number of rows/give user option to repeat the program

Posted 12 August 2014 - 09:22 PM

Please post the errors exactly as they appear.
Was This Post Helpful? 0
  • +
  • -

#5 hadamic   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 15
  • Joined: 11-August 14

Re: Ask user for number of rows/give user option to repeat the program

Posted 12 August 2014 - 09:35 PM

Sorry about that I finally figured it out anyway.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1