Please help! Nested while loops and asterisk triangles!
Write program which prints the following four triangle patterns with four different user
defined methods. Test your program by calling each of these four methods from the main method. Use different parameter values to test your methods. Assume the parameter of each method is an integer >= 5 and <= 25.
NOTE: Each of your methods is required to use two nested while loops to print its triangle pattern. Write your user defined methods in the same class as your main method and before your main method.
firstPattern(5); (call to firstPattern with parameter 5 produces this right triangle
with height 5 and base)
*
**
***
****
*****
secondPattern(5);
*****
****
***
**
*
thirdPattern(5);
*
**
***
****
*****
fourthPattern(5);
*****
****
***
**
*
Re: Please help! Nested while loops and asterisk triangles!
Please post what you've done with any and all errors clearly - we are not a code service so just posting a homework assignment will get you nowhere. Further, search the forums - this homework assignment has been posted time and again on the forums. Lastly, scroll down to the bottom of the page and you will find a list of Similar Threads, many of which will provide you guidance.
Re: Please help! Nested while loops and asterisk triangles!
Re: Please help! Nested while loops and asterisk triangles!
Quote:
Originally Posted by
copeg
Please post what you've done with any and all errors clearly - we are not a code service so just posting a homework assignment will get you nowhere. Further, search the forums - this homework assignment has been posted time and again on the forums. Lastly, scroll down to the bottom of the page and you will find a list of Similar Threads, many of which will provide you guidance.
Hey I am new I have the same assignment and got stuck in it to but i tried searching like you said, but i can't seem to find anyone else who posted it. Can you point me in the right direction to the others that have posted this assignment so i can find out where to begin?
Re: Please help! Nested while loops and asterisk triangles!
here is what i have just need help converting it. I personally like for loops better than while
Code Java:
import java.io.*;
public class NewClass {
public static void main(String[] args) {
for (int i = 1; i <= 5; i++)
{
for (int k=i;k<5;k++)
{
System.out.print(" ");
}
for (int j = i; j > 0; j--)
{
System.out.print("*");
}
System.out.println(" ");
}
System.out.println("");
int f=1;
for (int i = 5; i>=1; i--)
{
for (int j = i; j >=1; j--)
{
System.out.print("*");
}
System.out.println(" ");
for (int k=0;k<f;k++)
{
System.out.print(" ");
}
f++;
}
}
}
not sure if its right i just typed it and tried my best but i no its incomplete compared the assignment. Can you guys tell me what i am doing wrong?
Re: Please help! Nested while loops and asterisk triangles!
import java.util.Scanner;
public class Lab10a
{
public static void firstPattern()
{
int parameter;
Scanner scan=new Scanner(System.in);
System.out.print("Enter parameter number:");
parameter=scan.nextInt();
int row=1;
while(row<=parameter)
{
int star=1;
while(star<=row)
{
System.out.print("*");
star++;
}
System.out.println();
row++;
}
}
this is what I have so far and it works right, i just dont know how to do the other shapes??
also, how do I make it work so that instead of asking for the parameter in each method it will ask once and use that parameter in all of the methods?
Re: Please help! Nested while loops and asterisk triangles!
I assume is what you have since it is the easiest. It prints 1 star upto N stars.
Is the opposite. It prints N stars down to 1 star.
Code java:
****
***
**
*
*
**
***
****
These two patterns are similar to the first two except you now have to print M spaces before the stars.
Re: Please help! Nested while loops and asterisk triangles!
how about in nested for loop?how to convert that?
Re: Please help! Nested while loops and asterisk triangles!
Quote:
Originally Posted by
Zerro900
here is what i have just need help converting it. I personally like for loops better than while
Code Java:
import java.io.*;
public class NewClass {
public static void main(String[] args) {
for (int i = 1; i <= 5; i++)
{
for (int k=i;k<5;k++)
{
System.out.print(" ");
}
for (int j = i; j > 0; j--)
{
System.out.print("*");
}
System.out.println(" ");
}
System.out.println("");
int f=1;
for (int i = 5; i>=1; i--)
{
for (int j = i; j >=1; j--)
{
System.out.print("*");
}
System.out.println(" ");
for (int k=0;k<f;k++)
{
System.out.print(" ");
}
f++;
}
}
}
not sure if its right i just typed it and tried my best but i no its incomplete compared the assignment. Can you guys tell me what i am doing wrong?
convert that in nested for loop?
Re: Please help! Nested while loops and asterisk triangles!
The code does use nested for loops. What are you asking? And why have you revived a 5 month old thread?
Re: Please help! Nested while loops and asterisk triangles!
Quote:
Originally Posted by
Junky
The code does use nested for loops. What are you asking? And why have you revived a 5 month old thread?
Because school just started, so we're going to see a massive inflow of homework dumps. Batten down the hatches!
Re: Please help! Nested while loops and asterisk triangles!
Check this !!!!!!!!!!!!!!!!!!!!!!!!!
(spoonfeeding removed by KevinWorkman)
Re: Please help! Nested while loops and asterisk triangles!
Quote:
Originally Posted by
AJAXx195
Check this !!!!!!!!!!!!!!!!!!!!!!!!!
Please read this: http://www.javaprogrammingforums.com...n-feeding.html