In: Computer Science
How do I code a pyramid and a row (reference shapes below) into JAVA using user prompts (asking for different dimensions)? I am trying to make a nested loop code and keep running into these syntax issues. Will rate for proofed code!!!
Please try to leave lots of explanation.
Pyramid:
---*---
--**--
-***-
****
Row:
---***---
This is what I have so far:
public static void main(String[] args) {
// TODO Auto-generated method
stub
//start the scanner
Scanner scanner = new
Scanner(System.in);
final char SIDE_SYMB = '-';
final char MID_SYMB = '*';
//Start setting values for
pyramid
String inputStr = "";
char choice = ' ';
int numSymbols = -1, sideWidth =
-1, midWidth = -1;
do {
System.out.print("Please enter your selection");
System.out.print("'r' Print a row of symbols");
System.out.print("'p' Print out a pyramid");
System.out.print("'q' Quit program");
inputStr =
scanner.nextLine();
if
(inputStr.length() > 0) {
choice = inputStr.charAt(0); }
else { choice =
' '; }
//Set up the
switch
switch (choice)
{
//Code for the
row output
case 'r':
System.out.println("Width of the sides?");
sideWidth =
scanner.nextInt();
System.out.println("Width of the middle?");
midWidth =
scanner.nextInt();
scanner.nextLine();
case 'p':
System.out.println("Number of symbols on the
lowest layer?");
numSymbols = scanner.nextInt();
scanner.nextLine();
// Flush junk newline symbols
System.out.println();
System.out.println(buildPyramid(SIDE_SYMB,
MID_SYMB, numSymbols));
break;
case
'q':
System.out.println("Bye");
break;
//Send code back
to the top
default:
System.out.println("Please
choose a valid option from the menu.");
}
private static
String buildRow(char sideSymb, int sideWidth, char midSymb, int
midWidth){
String result = "";
for (int i = 0; i < sideWidth; i++){
result += sideSymb; }
for (int i = 0; i < midWidth; i++) {
result += midSymb; }
return result;}
//Separate code for building pyramid shape
private static
String buildPyramid(char sideSymb, char midSymb, int
numSymbols) {
String result = "";
int totalSymbOneRow = numSymbols;
for (int numStars = 1; numStars <= numSymbols;
numStars += 2)
{int numDashed = (totalSymbOneRow - numStars) / 2;
String buildRow(char sideSymb, char numDashed, char
midSymb, numStars);
result += row + "\n"; }
return result;
}
}
import java.util.*;
public class RandomNum {
public static void main(String[] args) {
// TODO Auto-generated method stub
//start the scanner
Scanner scanner = new Scanner(System.in);
final char SIDE_SYMB = '-';
final char MID_SYMB = '*';
//Start setting values for pyramid
String inputStr = "";
char choice = ' ';
int numSymbols = -1, sideWidth = -1, midWidth =
-1;
do {
System.out.print("Please enter your selection");
System.out.print("'r' Print a row of symbols");
System.out.print("'p' Print out a pyramid");
System.out.print("'q' Quit program");
inputStr = scanner.nextLine();
if (inputStr.length() > 0) {
choice = inputStr.charAt(0); }
else { choice = ' '; }
//Set up the switch
switch (choice)
{
//Code for the row output
case 'r':
System.out.println("Width of the sides?");
sideWidth = scanner.nextInt();
System.out.println("Width of the middle?");
midWidth = scanner.nextInt();
scanner.nextLine();
// break;
case 'p':
System.out.println("Number of symbols on the lowest
layer?");
numSymbols = scanner.nextInt();
scanner.nextLine();
// Flush junk newline symbols
System.out.println();
System.out.println(buildPyramid(SIDE_SYMB, MID_SYMB,
numSymbols));
break;
case 'q':
System.out.println("Bye");
break;
}
//Send code back to the top
// default:
// System.out.println("Please choose a valid option
from the menu.");
}while(choice=='q');
}
private static String buildRow(char sideSymb, int
sideWidth, char midSymb, int midWidth)
{
String result = "";
for (int i = 0; i < sideWidth; i++){
result += sideSymb; }
for (int i = 0; i < midWidth; i++) {
result += midSymb; }
return result;
}
//Separate code for building pyramid shape
public static String buildPyramid(char sideSymb, char
midSymb, int numSymbols)
{
String result = "";
int totalSymbOneRow = numSymbols;
for (int numStars = 1; numStars <= numSymbols;
numStars += 2)
{
int numDashed = (totalSymbOneRow -
numStars) / 2;
String row=buildRow(sideSymb,numDashed,
midSymb,numStars);//function call does not need datatypes
result += row + "\n";
}
return result;
}
}