homework troubles, any help would be appreciated
Homework question: Write a program that prompts the user to enter the diameter of the diamond and the character to use for drawing the diamond and then draws the diamond.
At the moment I just care about figuring out the diameter part.
Here is what i have so far:
Code :
/*
Scanner scan = new Scanner(System.in);
int diameter = 0;
System.out.print("Enter the diameter of diamond: ");
diameter = scan.nextInt();
*/
for(int i = 1; i < 6; i++)
{
for(int j = 1; j < 6 - i; j++)
System.out.print(" ");
for(int j = 1; j < 2 * i; j++)
System.out.print("*");
System.out.println();
}
for(int i = 1; i < 5; i++)
{
for(int j = 5; j > 5 - i; j--)
System.out.print(" ");
for(int j = 7; j >= i * 2 - 1; j--)
System.out.print("*");
System.out.println();
}
I figured if i did this first i could just replace some numbers with a variable and it would work but nothing I've tried has worked.
I just want to know if I'm heading in the right direction. If not can someone please push me in the right direction.
Thank you,
Steve
Re: homework troubles, any help would be appreciated
What is your current problem? Does the code compile? If not, post the error messages.
Does the code execute without errors? If not post the error messages.
Post what the code does output and show what is wrong with that output and show what you want the output to be.
Re: homework troubles, any help would be appreciated
Two things, you have hardcoded it to run only one specific diameter. Do you want to be able to run any diameter? What kinds of problems are you getting when you try to run it?
And second, adding comments to your code makes it many times easier to help you with your code.
Re: homework troubles, any help would be appreciated
Thank you guys for responding but I sat down with a my pen and a sheet of paper and did the loop step by step and figured it out and yes I need to add comments. Its something I need to work on.
here is the code if anyone is interested on how it works..
Code :
package project.pkg5part3;
import java.util.Scanner;
public class Project5Part3
{
public static void main(String[] args)
{
Scanner scan = new Scanner(System.in); // new object of scanner
//variables
int diameterOfDiamond;
String diamondCharacter;
boolean flag = true;
while(flag)
{
System.out.print("Enter the diameter of diamond: ");
diameterOfDiamond = scan.nextInt();
System.out.print("\nEnter the character to use for the drawing: ");
diamondCharacter = scan.next();
for(int i = 1; i <= (Math.ceil(diameterOfDiamond / 2.0)); i++)
{
for(int j = 1; j < (Math.ceil(diameterOfDiamond / 2.0) + 1) - i; j++)
System.out.print(" ");
for(int j = 1; j < 2 * i; j++)
System.out.print(diamondCharacter);
System.out.println();
}
for(int i = 1; i < (Math.ceil(diameterOfDiamond / 2.0) ); i++)
{
for(int j = (int) Math.ceil(diameterOfDiamond / 2.0); j > Math.ceil(diameterOfDiamond / 2.0) - i; j--)
System.out.print(" ");
for(int j = diameterOfDiamond - 2; j >= i * 2 - 1; j--)
System.out.print(diamondCharacter);
System.out.println();
}
System.out.println("Would you like to run this program again(yes/no): ");
if(scan.next().equalsIgnoreCase("no"))
flag = false;
}
}
}