Printing a Diamond shape Problem.
I am new to Java and have to do this for a class:
Write a program that displays diamonds as text using a character provided by the user. For example, a diamond of height 6 constructed from asterisks will display as follows:
*
***
*****
*****
***
*
Besides specifying a character, the user will specify one of three possible heights for each diamond – 6, 12, or 22 – by providing one of the strings “short”, “average”, or “tall”.
Write the following methods in the program:
• Public static int checkSize(String size) – This method will return 6 if size equals “short”, 12 if size equals “average”, 22 if size equals “tall”, or -1 otherwise. Be sure that the comparison is not case sensitive.
• Public static void displayPattern (int size, char ch) – This method will display a diamond of height size constructed from pattern character ch.
Use a do-while loop in the main method to verify that the diamond size that user enters must be “short”, “average”, or “tall”. If invalid string is input, the program should ask for new input until the valid input is given.
All of the help I found online was to make diamond shapes off of predetermined values for the shape. so im having some difficulty doing it how the assignment asks for it.
my code thus far:
Code Java:
import java.util.Scanner;
public class Diamonds
{
public static void main(String[] args)
{
Scanner kb = new Scanner(System.in);
do
{
System.out.println("enter diamond size (short, average, or tall):");
String size = kb.nextString();
equalIgnoreCase(size);
}while (size != "short" || "average" || "tall");
int s = checkSize(size);
System.out.println("enter pattern character:");
Char p = kb.nextChar();
displayPattern(s,p);
}
public static int checkSize(String size)
{
if (size = "short")
{
return 6;
}
else if (size = "average")
{
return 12;
}
else if (size = "tall")
{
return 22;
}
else
{
return -1;
}
}
public static void displayPattern (int size, char ch)
{
for (int s = 6; s <= n; s++)
{
for (int j = 0; j < (n - s); j++)
System.out.print(" ");
for (int j = 1; j <= s; j++)
System.out.print(p);
for (int k = 1; k < s; k++)
System.out.print(p);
System.out.println();
}
for (int s = n - 1; s >= 1; s--)
{
for (int j = 0; j < (n - s); j++)
System.out.print(" ");
}
}
im getting a bunch of errors stating that java compiler can "not find symbols" for n and p
any help would be great.
thankyou
Re: Printing a Diamond shape Problem.
the diamond shape didnt come out as a diamond.
should be like
---*
--***
-****
******
******
-****
--***
---*
Re: Printing a Diamond shape Problem.
Try this for the diamond symbol
* Spoon feeding removed by administrator *
Re: Printing a Diamond shape Problem.
@reddy1754
How does giving code to the OP help the OP think through his problem and come up with a solution?
See: http://www.javaprogrammingforums.com...n-feeding.html
If you are going to post code, you should add comments to it describing its logic and how you figured out the solution so the OP can learn the process of solving problems.
Copy and pasting does not teach programming.
Re: Printing a Diamond shape Problem.
Quote:
Originally Posted by
Sev
im getting a bunch of errors stating that java compiler can "not find symbols" for n and p
Where in the code have you declared the n variable?
You are getting the error with p because it is unreachable in the part of code you are requesting it. Try declaring it above the main method as public static.
Looking over your code, this is the least of your worries.
Code like size = "short" is not going to work either. You need to read up on the basics.
I suggest you read:
http://www.javaprogrammingforums.com...-mistakes.html
What IDE do you use? Eclipse will point out all of this errors to you.